Disclaimer

This work is very preliminary as I get back into the coding swing of things. Data wrangling and figure generation will be done via R, but the rest of the project will be done using good ol’ microsoft products. This is just an entry point into data crunching and should by no means be considered a final product.

Also, I’m not great at this but whatever. I could automate this, but I’ll figure that out shortly!

Need to update figures & analyze sites for seasonal trends

Methodology

SNOTEL data was provided by the NRCS. Data was cleaned by removing outliers that are likely implausible; any year with more than 15 observations missing was removed. Temperatures were adjusted using the Morrisey method for stations identified by Ma et al (2019) due to SNOTEL temperature sensor changes, with the adjustment applied to pre-sensor change data. Daily mean observations were detrended to determine whether values were increasing or decreasing from the entire time series trend. Daily mean temperatures were first averaged by water year, with all water year means then averaged by day of water year. The mean temperature by day for the period of record was averaged. To find the standard deviation, the daily mean temperatures by water year was subtracted from the averaged mean temperature by day for the period of record. All water year means averaged by day of water year were subtracted from the temperature mean. The resulting values were then added together to find the “residual” of the daily mean temperatures by water year. The standard deviation was then computed from those residuals, with trends analyzed by Mann‐Kendall significance test and Theil‐Sen’s rate of change. Significant trends are identified with p-values of less than 0.10.

Morrisey Method

The Morrisey Method is taken from Ma, Fassnacht and Kampf..

In R script: T(adjusted) = 5.3x10(-7)xT(old)4+3.72x10(-5)xT(old)3-2.16x10(-3)xT(old)2-7.32x10^(-2)xT(old)+1.37

In the Ma et al. spreadsheet, H1 is Morrisey, H2 is Oiler

01/03/2023 update

Analyzing by season for each station’s timeseries, using the shifted mean of the time series, then sigmoidal detrending. Standard deviation is taken with the relevant days of the water year.

San Juan Area SNOTEL sites:

Beartown 327 Original 4/18/2005

Cascade #2 389 Morrisey 6/18/2004

Cumbres Trestle 431 Oyler -> Morrisey 6/8/2005

Idarado 538 Morrisey 7/12/2004

Lone Cone 589 Oyler -> Morrisey 6/22/2005

Middle Creek 624 Morrisey 6/26/2006

Mineral Creek 629 Oyler ?? -> Morrisey 6/22/2004

Molas Lake 632 NSCE-Morrisey, Bias-Original 10/2/2003

Red Mountain Pass 713 Morrisey 8/18/2004

Scotch Creek 739 Morrisey 6/15/2004

Slumgullion 762 Morrisey 6/26/2006

Spud Mountain 780 Morrisey 6/28/2004

Stump Lakes 797 Morrisey 7/22/2005

Upper Rio Grande 839 Oyler -> Morrisey 5/26/2004 *Why is this in red?

Upper San Juan 840 Morrisey 4/7/2004

Vallecito 843 Morrisey 7/22/2005

Wolf Creek Summit 874 Morrisey 7/12/2004

Data from thesis research:


SNOTEL_san_juan_Area <- snotel_download(site_id = c(327, 389, 431, 538, 589, 624, 629, 632, 713, 739, 762, 780, 797, 839, 840, 843, 874), path = tempdir('../data'), internal = TRUE)

write.csv(SNOTEL_san_juan_Area,"C:/Users/13074/Documents/ESS580/thesis_project/San_Juan_area/data_raw/snotel_san_juans.csv", row.names = FALSE) #write in the raw data

Beartown 327

Original

SNOTEL_san_juan_Area <- read.csv("C:/Users/13074/Documents/ESS580/thesis_project/San_Juan_area/data_raw/snotel_san_juans.csv", header = TRUE)

snotel_327 <- SNOTEL_san_juan_Area %>% 
  filter(site_id == "327")
#str(snotel_327) # check the date, usually a character.  

snotel_327$Date <- as.Date(snotel_327$date) #change date from character to date format, capitalize to work with Water year functon from NWIS.

#THIS WILL CHANGE FOR EACH STATION
snotel_327_clean <- snotel_327 %>% # filter for the timeframe
  filter(Date >= "1983-08-10" & Date <= "2022-09-30") %>%
  #filter(temperature_mean >= -30 & temperature_mean <= 20) %>% # removing outliers   
  addWaterYear() %>% 
  mutate(daymonth = format(as.Date(Date), "%d-%m")) %>% 
  na.omit()

#adding water day using difftime (SUPER COOL. example from [this](https://stackoverflow.com/questions/48123049/create-day-index-based-on-water-year))

snotel_327_clean <- snotel_327_clean %>% 
  group_by(waterYear)%>% 
  mutate(waterDay = (as.integer(difftime(Date, ymd(paste0(waterYear - 1 ,'-09-30')), units = "days"))))
# Check for outliers

ggplot(snotel_327_clean, aes(x = Date, y = temperature_mean)) +
  geom_point() + #lwd = 2) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature (°C)') + 
  xlab('Date')

snotel_327_clean <- snotel_327_clean %>% 
  mutate(temp_diff = abs(temperature_min - temperature_max)) %>% 
  filter(temperature_min > -40)
ggplot(snotel_327_clean, aes(x = Date, y = temp_diff)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  #geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature varience (°C)') + 
  xlab('Date')

Per Steven’s advice: :If there are more than 15 missing days, then…remove that year”

# filtering for temperature anomalies
snotel_327_cull_count <- snotel_327_clean %>% 
  filter(temperature_min < -40) %>% 
  count(waterYear)

snotel_327_cull_count
## # A tibble: 0 x 2
## # Groups:   waterYear [0]
## # ... with 2 variables: waterYear <dbl>, n <int>
# filtering for too few observations in a year
snotel_327_cull_count_days <- snotel_327_clean %>% 
  group_by(waterYear) %>% 
  count(waterYear) %>% 
  filter(n < 350)

snotel_327_cull_count_days
## # A tibble: 3 x 2
## # Groups:   waterYear [3]
##   waterYear     n
##       <dbl> <int>
## 1      1983    52
## 2      1994   337
## 3      2003   346

1983, 1994, 2003 need to be culled.

snotel_327_clean_culled <- snotel_327_clean %>% 
  filter(waterYear != "1983" & waterYear != "1994" & waterYear != "2003") #%>% 
  #filter(temperature_mean > -49)

Beartown (327) NOT ADJUSTED.

2005-04-18 installed ext range sensor. Original

snotel_327_adjusted <- snotel_327_clean_culled %>%
  mutate(temp_ad = if_else(Date < "2005-04-18", ((5.3*10^(-7))*(temperature_mean^(4))+(3.72*10^(-5))*(temperature_mean^(3))-(2.16*10^(-3))*(temperature_mean^(2))-(7.32*10^(-2))*(temperature_mean)+1.37)+temperature_mean, temperature_mean))

Non-corrected

327 Detrended

#using the clean culled df:

#average water year temperature

yearly_wy_aver_327 <- snotel_327_adjusted %>% 
  group_by(waterYear) %>% 
  mutate(aver_ann_temp = mean(temperature_mean))

#Average temperature by day for all water years:

daily_wy_aver_327 <- yearly_wy_aver_327 %>% 
  group_by(daymonth) %>% 
  mutate(aver_day_temp = mean(aver_ann_temp))

#average mean temperature by day for the period of record:

daily_wy_aver_327 <- daily_wy_aver_327 %>% 
  group_by(daymonth) %>% 
  mutate(all_ave_temp = mean(daily_wy_aver_327$aver_day_temp))

# try to show all years as means. 
daily_wy_aver2_327 <-daily_wy_aver_327 %>% 
  group_by(waterDay) %>%
  mutate(date_temp = mean(temperature_mean))
  

daily_wy_aver2_327$date_temp <- signif(daily_wy_aver2_327$date_temp,3) #reduce the sig figs


ggplot(daily_wy_aver2_327, aes(x = waterDay, y = date_temp))+
  geom_line(size= 0.7) +
  theme_few() +
  ylab('Average Daily temperature (°C)') + 
  xlab('Day of water year')

327 SD

standard_dev_327 <- daily_wy_aver_327 %>% 
  group_by(waterYear) %>% 
  mutate(residual = (all_ave_temp-aver_ann_temp)+temperature_mean-aver_day_temp) %>% 
  mutate(deviation = abs(residual-lag(residual)))

standard_dev_all_327 <- standard_dev_327 %>% 
  group_by(waterYear) %>% 
  mutate(nmbr = n())

standard_dev_all_327 <- standard_dev_all_327 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

standard_dev_all_327 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1984 8.215357
1985 8.122497
1986 7.129599
1987 7.619777
1988 8.257318
1989 8.044976
1990 7.675963
1991 7.684970
1992 7.079517
1993 7.642300
1995 7.304999
1996 7.681058
1997 7.711950
1998 8.105700
1999 6.825322
2000 7.526413
2001 7.912097
2002 8.099855
2004 7.622635
2005 7.225545
2006 7.107924
2007 7.426444
2008 7.802192
2009 6.893765
2010 7.820347
2011 7.632307
2012 7.466342
2013 8.000100
2014 7.260505
2015 6.671562
2016 7.474460
2017 7.072348
2018 6.933787
2019 7.658054
2020 7.769482
2021 7.841403
2022 7.318492
ggplot(standard_dev_all_327, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 327 average temperatures for water years 2005-2021

MK & SS for 327 (non-corrected)

sd_mk_327 <- mk.test(standard_dev_all_327$sd_2)
print(sd_mk_327)
## 
##  Mann-Kendall trend test
## 
## data:  standard_dev_all_327$sd_2
## z = -1.818, n = 37, p-value = 0.06907
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
## -140.0000000 5846.0000000   -0.2102102
sd_sens_327 <- sens.slope(standard_dev_all_327$sd_2)
print(sd_sens_327)
## 
##  Sen's slope
## 
## data:  standard_dev_all_327$sd_2
## z = -1.818, n = 37, p-value = 0.06907
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.025461316  0.001275883
## sample estimates:
## Sen's slope 
## -0.01223021

Summer and Winter SD NOT-CORRECTED

Summer

summer_standard_dev_all_327 <- standard_dev_327 %>%
  filter(waterDay >= 244 & waterDay <= 335) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())

summer_standard_dev_all_327 <- summer_standard_dev_all_327 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

summer_standard_dev_all_327 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1984 2.662470
1985 2.202079
1986 2.315446
1987 2.221928
1988 2.666407
1989 2.908971
1990 2.435507
1991 2.288870
1992 2.736828
1993 3.073040
1995 3.177087
1996 2.093670
1997 2.176252
1998 3.039807
1999 2.583979
2000 1.611760
2001 2.304035
2002 2.156006
2004 2.225919
2005 2.858557
2006 1.830347
2007 2.362229
2008 2.159473
2009 2.619961
2010 2.177014
2011 1.814920
2012 1.545735
2013 1.735480
2014 1.867382
2015 2.209308
2016 2.440044
2017 1.787620
2018 1.602691
2019 2.400103
2020 2.288616
2021 2.153505
2022 2.028381
ggplot(summer_standard_dev_all_327, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 327 average summer temperatures for water years 2005-2021

summer MK & SS for 327 (non-corrected)

summer_sd_mk_327 <- mk.test(summer_standard_dev_all_327$sd_2)
print(summer_sd_mk_327)
## 
##  Mann-Kendall trend test
## 
## data:  summer_standard_dev_all_327$sd_2
## z = -2.7858, n = 37, p-value = 0.00534
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
## -214.0000000 5846.0000000   -0.3213213
summer_sd_sens_327 <- sens.slope(summer_standard_dev_all_327$sd_2)
print(summer_sd_sens_327)
## 
##  Sen's slope
## 
## data:  summer_standard_dev_all_327$sd_2
## z = -2.7858, n = 37, p-value = 0.00534
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.030197599 -0.004550044
## sample estimates:
## Sen's slope 
## -0.01753962

Winter

winter_standard_dev_all_327 <- standard_dev_327 %>%
  filter(waterDay >= 32 & waterDay <= 182) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())

winter_standard_dev_all_327 <- winter_standard_dev_all_327 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

winter_standard_dev_all_327 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1984 4.948620
1985 4.804176
1986 4.485164
1987 4.341123
1988 4.957619
1989 5.497259
1990 5.027247
1991 4.699816
1992 3.461224
1993 3.676274
1995 4.234140
1996 4.899380
1997 4.966943
1998 4.384582
1999 4.129867
2000 4.938855
2001 3.977088
2002 4.803227
2004 5.176728
2005 3.988333
2006 4.530121
2007 5.073336
2008 5.395287
2009 4.498157
2010 4.341478
2011 5.174805
2012 4.236838
2013 5.575214
2014 4.008632
2015 4.510259
2016 4.763128
2017 4.971458
2018 4.254628
2019 4.011225
2020 4.438512
2021 4.336327
2022 4.613189
ggplot(winter_standard_dev_all_327, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 327 average winter temperatures for water years 2005-2021

winter MK & SS for 327 (non-corrected)

winter_sd_mk_327 <- mk.test(winter_standard_dev_all_327$sd_2)
print(winter_sd_mk_327)
## 
##  Mann-Kendall trend test
## 
## data:  winter_standard_dev_all_327$sd_2
## z = -0.45776, n = 37, p-value = 0.6471
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##             S          varS           tau 
##  -36.00000000 5846.00000000   -0.05405405
winter_sd_sens_327 <- sens.slope(winter_standard_dev_all_327$sd_2)
print(winter_sd_sens_327)
## 
##  Sen's slope
## 
## data:  winter_standard_dev_all_327$sd_2
## z = -0.45776, n = 37, p-value = 0.6471
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.02107872  0.01135756
## sample estimates:
##  Sen's slope 
## -0.004978217

Cascade #2 389

Morrisey 6/18/2004

snotel_389 <- SNOTEL_san_juan_Area %>% 
  filter(site_id == "389")
#str(snotel_389) # check the date, usually a character.  

snotel_389$Date <- as.Date(snotel_389$date) #change date from character to date format, capitalize to work with Water year functon from NWIS.

#THIS WILL CHANGE FOR EACH STATION
snotel_389_clean <- snotel_389 %>% # filter for the timeframe
  filter(Date >= "1979-10-01" & Date <= "2022-09-30") %>%
  #filter(temperature_mean >= -30 & temperature_mean <= 20) %>% # removing outliers   
  addWaterYear() %>% 
  mutate(daymonth = format(as.Date(Date), "%d-%m")) %>% 
  na.omit()

#adding water day using difftime (SUPER COOL. example from [this](https://stackoverflow.com/questions/48123049/create-day-index-based-on-water-year))

snotel_389_clean <- snotel_389_clean %>% 
  group_by(waterYear)%>% 
  mutate(waterDay = (as.integer(difftime(Date, ymd(paste0(waterYear - 1 ,'-09-30')), units = "days"))))
# Check for outliers

ggplot(snotel_389_clean, aes(x = Date, y = temperature_mean)) +
  geom_point() + #lwd = 2) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature (°C)') + 
  xlab('Date')

snotel_389_clean <- snotel_389_clean %>% 
  mutate(temp_diff = abs(temperature_min - temperature_max)) %>% 
  filter(temperature_min > -40) %>% 
  filter(temperature_mean > -40)
ggplot(snotel_389_clean, aes(x = Date, y = temp_diff)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  #geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature varience (°C)') + 
  xlab('Date')

temp_389_xts <- xts(snotel_389_clean$temperature_mean, order.by = snotel_389_clean$Date)

dygraph(temp_389_xts) %>%
  dyAxis("y", label = "Daily mean temperature (°C)") 

Per Steven’s advice: :If there are more than 15 missing days, then…remove that year”

# filtering for temperature anomalies
snotel_389_cull_count <- snotel_389_clean %>% 
  filter(temperature_min > -40) %>% 
  count(waterYear)

snotel_389_cull_count
## # A tibble: 40 x 2
## # Groups:   waterYear [40]
##    waterYear     n
##        <dbl> <int>
##  1      1983   325
##  2      1984   350
##  3      1985   350
##  4      1986   365
##  5      1987   358
##  6      1988   366
##  7      1989   330
##  8      1990   348
##  9      1991   364
## 10      1992   366
## # ... with 30 more rows
# filtering for too few observations in a year
snotel_389_cull_count_days <- snotel_389_clean %>% 
  group_by(waterYear) %>% 
  count(waterYear) %>% 
  filter(n < 350)

snotel_389_cull_count_days
## # A tibble: 9 x 2
## # Groups:   waterYear [9]
##   waterYear     n
##       <dbl> <int>
## 1      1983   325
## 2      1989   330
## 3      1990   348
## 4      1994   310
## 5      2006   330
## 6      2010   334
## 7      2014   314
## 8      2015   185
## 9      2016   334

1983, 1989, 1990, 1994, 2006, 2010, 2014, 2015, 2016 need to be culled.

snotel_389_clean_culled <- snotel_389_clean %>% 
  filter(waterYear != "1983" & waterYear != "1990" & waterYear != "1994" & waterYear != "2006" & waterYear != "2010" & waterYear != "2014" & waterYear != "2015" & waterYear != "2016") #%>% 
  #filter(temperature_mean > -49)

Cascade #2 (389) NOT ADJUSTED.

Morrisey 6/18/2004

snotel_389_adjusted <- snotel_389_clean_culled %>%
  mutate(temp_ad = if_else(Date < "2004-06-18", ((5.3*10^(-7))*(temperature_mean^(4))+(3.72*10^(-5))*(temperature_mean^(3))-(2.16*10^(-3))*(temperature_mean^(2))-(7.32*10^(-2))*(temperature_mean)+1.37)+temperature_mean, temperature_mean))

Non-corrected

389 Detrended

#using the clean culled df:

#average water year temperature

yearly_wy_aver_389 <- snotel_389_adjusted %>% 
  group_by(waterYear) %>% 
  mutate(aver_ann_temp = mean(temperature_mean))

#Average temperature by day for all water years:

daily_wy_aver_389 <- yearly_wy_aver_389 %>% 
  group_by(daymonth) %>% 
  mutate(aver_day_temp = mean(aver_ann_temp))

#average mean temperature by day for the period of record:

daily_wy_aver_389 <- daily_wy_aver_389 %>% 
  group_by(daymonth) %>% 
  mutate(all_ave_temp = mean(daily_wy_aver_389$aver_day_temp))

# try to show all years as means. 
daily_wy_aver2_389 <-daily_wy_aver_389 %>% 
  group_by(waterDay) %>%
  mutate(date_temp = mean(temperature_mean))
  

daily_wy_aver2_389$date_temp <- signif(daily_wy_aver2_389$date_temp,3) #reduce the sig figs


ggplot(daily_wy_aver2_389, aes(x = waterDay, y = date_temp))+
  geom_line(size= 0.7) +
  theme_few() +
  ylab('Average Daily temperature (°C)') + 
  xlab('Day of water year')

389 SD

standard_dev_389 <- daily_wy_aver_389 %>% 
  group_by(waterYear) %>% 
  mutate(residual = (all_ave_temp-aver_ann_temp)+temperature_mean-aver_day_temp) %>% 
  mutate(deviation = abs(residual-lag(residual)))

standard_dev_all_389 <- standard_dev_389 %>% 
  group_by(waterYear) %>% 
  mutate(nmbr = n())

standard_dev_all_389 <- standard_dev_all_389 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

standard_dev_all_389 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1984 10.776321
1985 7.879611
1986 10.887499
1987 9.408888
1988 10.711609
1989 10.418326
1991 10.101153
1992 8.637142
1993 8.884440
1995 9.371322
1996 9.429341
1997 10.027003
1998 9.698491
1999 9.029327
2000 9.288091
2001 10.630687
2002 10.346726
2003 9.727202
2004 9.282545
2005 8.472227
2007 9.686674
2008 9.541070
2009 8.659960
2011 9.334867
2012 9.418612
2013 9.751020
2017 9.144382
2018 8.911243
2019 9.427120
2020 9.880776
2021 9.863539
2022 9.441803
ggplot(standard_dev_all_389, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 389 average temperatures for water years 2005-2021

MK & SS for 389 (non-corrected)

sd_mk_389 <- mk.test(standard_dev_all_389$sd_2)
print(sd_mk_389)
## 
##  Mann-Kendall trend test
## 
## data:  standard_dev_all_389$sd_2
## z = -0.95677, n = 32, p-value = 0.3387
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
##  -60.0000000 3802.6666667   -0.1209677
sd_sens_389 <- sens.slope(standard_dev_all_389$sd_2)
print(sd_sens_389)
## 
##  Sen's slope
## 
## data:  standard_dev_all_389$sd_2
## z = -0.95677, n = 32, p-value = 0.3387
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.04702986  0.01336703
## sample estimates:
## Sen's slope 
## -0.01646233

Corrected

389 Detrended (corrected)

#using the clean culled df:
#average water year temperature
yearly_wy_aver_389_ad <- snotel_389_adjusted %>% 
  group_by(waterYear) %>% 
  mutate(aver_ann_temp_ad = mean(temp_ad))
#Average temperature by day for all water years:
daily_wy_aver_389_ad <- yearly_wy_aver_389_ad %>% 
  group_by(daymonth) %>% 
  mutate(aver_day_temp_ad = mean(aver_ann_temp_ad))
#average mean temperature by day for the period of record:
daily_wy_aver_389_ad <- daily_wy_aver_389_ad %>% 
  group_by(daymonth) %>% 
  mutate(all_ave_temp_ad = mean(daily_wy_aver_389_ad$aver_day_temp_ad))
# try to show all years as means. 
daily_wy_aver2_389_ad <-daily_wy_aver_389_ad %>% 
  group_by(waterDay) %>%
  mutate(date_temp_ad = mean(temp_ad))
  
daily_wy_aver2_389_ad$date_temp_ad <- signif(daily_wy_aver2_389_ad$date_temp_ad,3) #reduce the sig figs
ggplot(daily_wy_aver2_389_ad, aes(x = waterDay, y = date_temp_ad))+
  geom_line(size= 0.7) +
  theme_few() +
  ylab('Average Daily temperature (°C)') + 
  xlab('Day of water year')

389 SS (corrected)

standard_dev_389_ad <- daily_wy_aver_389_ad %>% 
  group_by(waterYear) %>% 
  #filter(waterYear >= 1987 & waterYear <= 2021) %>% 
  mutate(residual = (all_ave_temp_ad-aver_ann_temp_ad)+temp_ad-aver_day_temp_ad) %>% 
  mutate(deviation = abs(residual-lag(residual)))
standard_dev_all_389_ad <- standard_dev_389_ad %>% 
  group_by(waterYear) %>% 
  mutate(nmbr = n())
standard_dev_all_389_ad <- standard_dev_all_389_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
standard_dev_all_389_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1984 10.061639
1985 7.175032
1986 9.930924
1987 8.682951
1988 9.868631
1989 9.733860
1991 9.472969
1992 8.002025
1993 8.283300
1995 8.657379
1996 8.697682
1997 9.311693
1998 8.929771
1999 8.328247
2000 8.510889
2001 9.808164
2002 9.563945
2003 8.940614
2004 8.587303
2005 8.472294
2007 9.686174
2008 9.541322
2009 8.659292
2011 9.334237
2012 9.418205
2013 9.750812
2017 9.144257
2018 8.911176
2019 9.426664
2020 9.880403
2021 9.863015
2022 9.441185
ggplot(standard_dev_all_389_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 389 average temperatures for water years 1986-2021

MK & SS 389 (corrected)

sd_mk_389_ad <- mk.test(standard_dev_all_389_ad$sd_2)
print(sd_mk_389_ad)
## 
##  Mann-Kendall trend test
## 
## data:  standard_dev_all_389_ad$sd_2
## z = 1.0216, n = 32, p-value = 0.307
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
##   64.0000000 3802.6666667    0.1290323
sd_sens_389_ad <- sens.slope(standard_dev_all_389_ad$sd_2)
print(sd_sens_389_ad)
## 
##  Sen's slope
## 
## data:  standard_dev_all_389_ad$sd_2
## z = 1.0216, n = 32, p-value = 0.307
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.01140359  0.04896578
## sample estimates:
## Sen's slope 
##  0.01785583

Summer and Winter SD NOT-CORRECTED

Summer

summer_standard_dev_all_389 <- standard_dev_389 %>%
  filter(waterDay >= 244 & waterDay <= 335) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())

summer_standard_dev_all_389 <- summer_standard_dev_all_389 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

summer_standard_dev_all_389 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1984 4.131364
1985 4.257379
1986 3.848435
1987 3.614143
1988 3.354451
1989 4.543156
1991 3.020404
1992 3.926793
1993 4.103980
1995 5.062519
1996 3.153219
1997 3.410861
1998 5.284244
1999 4.009532
2000 4.436829
2001 4.030007
2002 3.962191
2003 5.549740
2004 4.148995
2005 4.429532
2007 3.942467
2008 4.324089
2009 3.848610
2011 3.430298
2012 3.449043
2013 2.904594
2017 3.263612
2018 3.535952
2019 3.722023
2020 3.409031
2021 3.270795
2022 3.671081
ggplot(summer_standard_dev_all_389, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 389 average summer temperatures for water years 2005-2021

summer MK & SS for 389 (non-corrected)

summer_sd_mk_389 <- mk.test(summer_standard_dev_all_389$sd_2)
print(summer_sd_mk_389)
## 
##  Mann-Kendall trend test
## 
## data:  summer_standard_dev_all_389$sd_2
## z = -1.7027, n = 32, p-value = 0.08862
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
## -106.0000000 3802.6666667   -0.2137097
summer_sd_sens_389 <- sens.slope(summer_standard_dev_all_389$sd_2)
print(summer_sd_sens_389)
## 
##  Sen's slope
## 
## data:  summer_standard_dev_all_389$sd_2
## z = -1.7027, n = 32, p-value = 0.08862
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.041171288  0.002937086
## sample estimates:
## Sen's slope 
## -0.01901402

Winter

winter_standard_dev_all_389 <- standard_dev_389 %>%
  filter(waterDay >= 32 & waterDay <= 182) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())

winter_standard_dev_all_389 <- winter_standard_dev_all_389 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

winter_standard_dev_all_389 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1984 6.807318
1985 3.153448
1986 6.840301
1987 5.057972
1988 5.626265
1989 7.320073
1991 7.125680
1992 4.806011
1993 5.375083
1995 5.764318
1996 5.731706
1997 6.143964
1998 4.682849
1999 5.427183
2000 5.321558
2001 4.480937
2002 5.832425
2003 4.736419
2004 5.346649
2005 4.555933
2007 6.249234
2008 5.541754
2009 5.861833
2011 5.740282
2012 5.003257
2013 5.862060
2017 6.814285
2018 5.115094
2019 5.138728
2020 4.581278
2021 5.408294
2022 5.905526
ggplot(winter_standard_dev_all_389, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 389 average winter temperatures for water years 2005-2021

winter MK & SS for 389 (non-corrected)

winter_sd_mk_389 <- mk.test(winter_standard_dev_all_389$sd_2)
print(winter_sd_mk_389)
## 
##  Mann-Kendall trend test
## 
## data:  winter_standard_dev_all_389$sd_2
## z = -0.47028, n = 32, p-value = 0.6382
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##             S          varS           tau 
##  -30.00000000 3802.66666667   -0.06048387
winter_sd_sens_389 <- sens.slope(winter_standard_dev_all_389$sd_2)
print(winter_sd_sens_389)
## 
##  Sen's slope
## 
## data:  winter_standard_dev_all_389$sd_2
## z = -0.47028, n = 32, p-value = 0.6382
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.04935199  0.02396237
## sample estimates:
## Sen's slope 
## -0.01311124

Summer and Winter SD CORRECTED

Summer

summer_standard_dev_all_389_ad <- standard_dev_389_ad %>% 
  filter(waterDay >= 244 & waterDay <= 335) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())
summer_standard_dev_all_389_ad <- summer_standard_dev_all_389_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
summer_standard_dev_all_389_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1984 3.707153
1985 3.821580
1986 3.504151
1987 3.239201
1988 3.007898
1989 4.079121
1991 2.704665
1992 3.527721
1993 3.690728
1995 4.549457
1996 2.826078
1997 3.054303
1998 4.754666
1999 3.595035
2000 3.982629
2001 3.619493
2002 3.554444
2003 4.987745
2004 4.029605
2005 4.428399
2007 3.941918
2008 4.323491
2009 3.847683
2011 3.429423
2012 3.447913
2013 2.904944
2017 3.263123
2018 3.535436
2019 3.722214
2020 3.407740
2021 3.271523
2022 3.671683
ggplot(summer_standard_dev_all_389_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 389 average summer temperatures for water years 1986-2021

summer MK & SS 389 (corrected)

summer_sd_mk_389_ad <- mk.test(summer_standard_dev_all_389_ad$sd_2)
print(summer_sd_mk_389_ad)
## 
##  Mann-Kendall trend test
## 
## data:  summer_standard_dev_all_389_ad$sd_2
## z = -0.21081, n = 32, p-value = 0.833
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##             S          varS           tau 
##  -14.00000000 3802.66666667   -0.02822581
summer_sd_sens_389_ad <- sens.slope(summer_standard_dev_all_389_ad$sd_2)
print(summer_sd_sens_389_ad)
## 
##  Sen's slope
## 
## data:  summer_standard_dev_all_389_ad$sd_2
## z = -0.21081, n = 32, p-value = 0.833
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.01896748  0.02093255
## sample estimates:
##  Sen's slope 
## -0.003592301

Winter

winter_standard_dev_all_389_ad <- standard_dev_389_ad %>% 
  filter(waterDay >= 32 & waterDay <= 182) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())
winter_standard_dev_all_389_ad <- winter_standard_dev_all_389_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
winter_standard_dev_all_389_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1984 6.718431
1985 3.000834
1986 6.380571
1987 4.860067
1988 5.385811
1989 7.204772
1991 7.067613
1992 4.628395
1993 5.210152
1995 5.538288
1996 5.504719
1997 5.934671
1998 4.486236
1999 5.207953
2000 5.006156
2001 4.351748
2002 5.602628
2003 4.528801
2004 5.131391
2005 4.557664
2007 6.249201
2008 5.543631
2009 5.861277
2011 5.741768
2012 5.003529
2013 5.863490
2017 6.815681
2018 5.116945
2019 5.139629
2020 4.582440
2021 5.409727
2022 5.906230
ggplot(winter_standard_dev_all_389_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 389 average winter temperatures for water years 1986-2021

winter MK & SS 389 (corrected)

winter_sd_mk_389_ad <- mk.test(winter_standard_dev_all_389_ad$sd_2)
print(winter_sd_mk_389_ad)
## 
##  Mann-Kendall trend test
## 
## data:  winter_standard_dev_all_389_ad$sd_2
## z = -0.016216, n = 32, p-value = 0.9871
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##             S          varS           tau 
## -2.000000e+00  3.802667e+03 -4.032258e-03
winter_sd_sens_389_ad <- sens.slope(winter_standard_dev_all_389_ad$sd_2)
print(winter_sd_sens_389_ad)
## 
##  Sen's slope
## 
## data:  winter_standard_dev_all_389_ad$sd_2
## z = -0.016216, n = 32, p-value = 0.9871
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.03909454  0.03478859
## sample estimates:
##   Sen's slope 
## -0.0003513145

Cumbres Trestle 431

Oyler -> Morrisey 6/8/2005

snotel_431 <- SNOTEL_san_juan_Area %>% 
  filter(site_id == "431")
#str(snotel_431) # check the date, usually a character.  

snotel_431$Date <- as.Date(snotel_431$date) #change date from character to date format, capitalize to work with Water year functon from NWIS.

#THIS WILL CHANGE FOR EACH STATION
snotel_431_clean <- snotel_431 %>% # filter for the timeframe
  filter(Date >= "1979-10-01" & Date <= "2022-09-30") %>%
  #filter(temperature_mean >= -30 & temperature_mean <= 20) %>% # removing outliers   
  addWaterYear() %>% 
  mutate(daymonth = format(as.Date(Date), "%d-%m")) %>% 
  na.omit()

#adding water day using difftime (SUPER COOL. example from [this](https://stackoverflow.com/questions/48123049/create-day-index-based-on-water-year))

snotel_431_clean <- snotel_431_clean %>% 
  group_by(waterYear)%>% 
  mutate(waterDay = (as.integer(difftime(Date, ymd(paste0(waterYear - 1 ,'-09-30')), units = "days"))))
# Check for outliers

ggplot(snotel_431_clean, aes(x = Date, y = temperature_mean)) +
  geom_point() + #lwd = 2) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature (°C)') + 
  xlab('Date')

snotel_431_clean <- snotel_431_clean %>% 
  mutate(temp_diff = abs(temperature_min - temperature_max)) %>% 
  filter(temperature_min > -40) %>% 
  filter(temperature_min < 25)
ggplot(snotel_431_clean, aes(x = Date, y = temp_diff)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  #geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature varience (°C)') + 
  xlab('Date')

Per Steven’s advice: :If there are more than 15 missing days, then…remove that year”

# filtering for temperature anomalies
snotel_431_cull_count <- snotel_431_clean %>% 
  filter(temperature_min > -40) %>% 
  count(waterYear)

snotel_431_cull_count
## # A tibble: 36 x 2
## # Groups:   waterYear [36]
##    waterYear     n
##        <dbl> <int>
##  1      1987     1
##  2      1988   341
##  3      1989   364
##  4      1990   348
##  5      1991   341
##  6      1992   312
##  7      1993   356
##  8      1994   317
##  9      1995   364
## 10      1996   364
## # ... with 26 more rows
# filtering for too few observations in a year
snotel_431_cull_count_days <- snotel_431_clean %>% 
  group_by(waterYear) %>% 
  count(waterYear) %>% 
  filter(n < 350)

snotel_431_cull_count_days
## # A tibble: 7 x 2
## # Groups:   waterYear [7]
##   waterYear     n
##       <dbl> <int>
## 1      1987     1
## 2      1988   341
## 3      1990   348
## 4      1991   341
## 5      1992   312
## 6      1994   317
## 7      2014   288

1987, 1988, 1990, 1991, 1992, 1994, 2014 need to be culled.

snotel_431_clean_culled <- snotel_431_clean %>% 
  filter(waterYear != "1987" & waterYear != "1988" & waterYear != "1990" & waterYear != "1991" & waterYear != "1992" & waterYear != "1994" & waterYear != "2014") #%>% 
  #filter(temperature_mean > -49)
ggplot(snotel_431_clean_culled, aes(x = Date, y = temperature_mean)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature (°C)') + 
  xlab('Date')

temp_431_xts <- xts(snotel_431_clean_culled$temperature_mean, order.by = snotel_431_clean_culled$Date)

dygraph(temp_431_xts) %>%
  dyAxis("y", label = "Daily mean temperature (°C)") 
snotel_431_clean_culled <- snotel_431_clean_culled %>% 
  filter(temperature_mean < 20)

temp_431_xts <- xts(snotel_431_clean_culled$temperature_mean, order.by = snotel_431_clean_culled$Date)

dygraph(temp_431_xts) %>%
  dyAxis("y", label = "Daily mean temperature (°C)") 

Cumbres Trestle 431

Oyler -> Morrisey 6/8/2005

snotel_431_adjusted <- snotel_431_clean_culled %>%
  mutate(temp_ad = if_else(Date < "2005-06-08", ((5.3*10^(-7))*(temperature_mean^(4))+(3.72*10^(-5))*(temperature_mean^(3))-(2.16*10^(-3))*(temperature_mean^(2))-(7.32*10^(-2))*(temperature_mean)+1.37)+temperature_mean, temperature_mean))

Non-corrected

431 Detrended

#using the clean culled df:

#average water year temperature

yearly_wy_aver_431 <- snotel_431_adjusted %>% 
  group_by(waterYear) %>% 
  mutate(aver_ann_temp = mean(temperature_mean))

#Average temperature by day for all water years:

daily_wy_aver_431 <- yearly_wy_aver_431 %>% 
  group_by(daymonth) %>% 
  mutate(aver_day_temp = mean(aver_ann_temp))

#average mean temperature by day for the period of record:

daily_wy_aver_431 <- daily_wy_aver_431 %>% 
  group_by(daymonth) %>% 
  mutate(all_ave_temp = mean(daily_wy_aver_431$aver_day_temp))

# try to show all years as means. 
daily_wy_aver2_431 <-daily_wy_aver_431 %>% 
  group_by(waterDay) %>%
  mutate(date_temp = mean(temperature_mean))
  

daily_wy_aver2_431$date_temp <- signif(daily_wy_aver2_431$date_temp,3) #reduce the sig figs


ggplot(daily_wy_aver2_431, aes(x = waterDay, y = date_temp))+
  geom_line(size= 0.7) +
  theme_few() +
  ylab('Average Daily temperature (°C)') + 
  xlab('Day of water year')

431 SD

standard_dev_431 <- daily_wy_aver_431 %>% 
  group_by(waterYear) %>% 
  mutate(residual = (all_ave_temp-aver_ann_temp)+temperature_mean-aver_day_temp) %>% 
  mutate(deviation = abs(residual-lag(residual)))

standard_dev_all_431 <- standard_dev_431 %>% 
  group_by(waterYear) %>% 
  mutate(nmbr = n())

standard_dev_all_431 <- standard_dev_all_431 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

standard_dev_all_431 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1989 8.484011
1993 8.101376
1995 7.750446
1996 7.952021
1997 8.217323
1998 8.600343
1999 7.226028
2000 8.164156
2001 8.669911
2002 9.005161
2003 8.459072
2004 8.306872
2005 8.304679
2006 7.417238
2007 7.937180
2008 8.259566
2009 7.296965
2010 8.418225
2011 8.231369
2012 8.081428
2013 8.537331
2015 7.219164
2016 7.791453
2017 7.322790
2018 7.113402
2019 7.880447
2020 8.046531
2021 8.095523
2022 7.621146
ggplot(standard_dev_all_431, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 431 average temperatures for water years 2005-2021

MK & SS for 431 (non-corrected)

sd_mk_431 <- mk.test(standard_dev_all_431$sd_2)
print(sd_mk_431)
## 
##  Mann-Kendall trend test
## 
## data:  standard_dev_all_431$sd_2
## z = -1.9321, n = 29, p-value = 0.05335
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
## -104.0000000 2842.0000000   -0.2561576
sd_sens_431 <- sens.slope(standard_dev_all_431$sd_2)
print(sd_sens_431)
## 
##  Sen's slope
## 
## data:  standard_dev_all_431$sd_2
## z = -1.9321, n = 29, p-value = 0.05335
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.041299369  0.001003281
## sample estimates:
## Sen's slope 
## -0.01890302

Corrected

431 Detrended (corrected)

#using the clean culled df:
#average water year temperature
yearly_wy_aver_431_ad <- snotel_431_adjusted %>% 
  group_by(waterYear) %>% 
  mutate(aver_ann_temp_ad = mean(temp_ad))
#Average temperature by day for all water years:
daily_wy_aver_431_ad <- yearly_wy_aver_431_ad %>% 
  group_by(daymonth) %>% 
  mutate(aver_day_temp_ad = mean(aver_ann_temp_ad))
#average mean temperature by day for the period of record:
daily_wy_aver_431_ad <- daily_wy_aver_431_ad %>% 
  group_by(daymonth) %>% 
  mutate(all_ave_temp_ad = mean(daily_wy_aver_431_ad$aver_day_temp_ad))
# try to show all years as means. 
daily_wy_aver2_431_ad <-daily_wy_aver_431_ad %>% 
  group_by(waterDay) %>%
  mutate(date_temp_ad = mean(temp_ad))
  
daily_wy_aver2_431_ad$date_temp_ad <- signif(daily_wy_aver2_431_ad$date_temp_ad,3) #reduce the sig figs
ggplot(daily_wy_aver2_431_ad, aes(x = waterDay, y = date_temp_ad))+
  geom_line(size= 0.7) +
  theme_few() +
  ylab('Average Daily temperature (°C)') + 
  xlab('Day of water year')

431 SS (corrected)

standard_dev_431_ad <- daily_wy_aver_431_ad %>% 
  group_by(waterYear) %>% 
  #filter(waterYear >= 1987 & waterYear <= 2021) %>% 
  mutate(residual = (all_ave_temp_ad-aver_ann_temp_ad)+temp_ad-aver_day_temp_ad) %>% 
  mutate(deviation = abs(residual-lag(residual)))
standard_dev_all_431_ad <- standard_dev_431_ad %>% 
  group_by(waterYear) %>% 
  mutate(nmbr = n())
standard_dev_all_431_ad <- standard_dev_all_431_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
standard_dev_all_431_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1989 7.914872
1993 7.518415
1995 7.173944
1996 7.365597
1997 7.634730
1998 7.970563
1999 6.682639
2000 7.537564
2001 8.039524
2002 8.344015
2003 7.813684
2004 7.737034
2005 7.599212
2006 7.418573
2007 7.938448
2008 8.260644
2009 7.298209
2010 8.419846
2011 8.232506
2012 8.082571
2013 8.538590
2015 7.220737
2016 7.793079
2017 7.324026
2018 7.114853
2019 7.881605
2020 8.047598
2021 8.096826
2022 7.622159
ggplot(standard_dev_all_431_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 431 average temperatures for water years 1986-2021

MK & SS 431 (corrected)

sd_mk_431_ad <- mk.test(standard_dev_all_431_ad$sd_2)
print(sd_mk_431_ad)
## 
##  Mann-Kendall trend test
## 
## data:  standard_dev_all_431_ad$sd_2
## z = 0.99418, n = 29, p-value = 0.3201
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
##   54.0000000 2842.0000000    0.1330049
sd_sens_431_ad <- sens.slope(standard_dev_all_431_ad$sd_2)
print(sd_sens_431_ad)
## 
##  Sen's slope
## 
## data:  standard_dev_all_431_ad$sd_2
## z = 0.99418, n = 29, p-value = 0.3201
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.01334615  0.03134200
## sample estimates:
## Sen's slope 
##  0.01029449

Summer and Winter SD NOT-CORRECTED

Summer

summer_standard_dev_all_431 <- standard_dev_431 %>%
  filter(waterDay >= 244 & waterDay <= 335) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())

summer_standard_dev_all_431 <- summer_standard_dev_all_431 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

summer_standard_dev_all_431 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1989 2.753414
1993 2.753498
1995 2.968036
1996 2.004188
1997 2.331962
1998 2.670393
1999 2.395906
2000 1.868394
2001 2.255163
2002 2.114542
2003 2.976102
2004 2.230182
2005 2.660337
2006 1.780228
2007 2.416832
2008 2.187852
2009 2.461896
2010 2.036498
2011 1.867116
2012 1.665295
2013 1.649738
2015 1.951124
2016 2.508294
2017 1.793169
2018 1.791966
2019 2.345594
2020 2.094093
2021 2.284195
2022 2.148142
ggplot(summer_standard_dev_all_431, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 431 average summer temperatures for water years 2005-2021

summer MK & SS for 431 (non-corrected)

summer_sd_mk_431 <- mk.test(summer_standard_dev_all_431$sd_2)
print(summer_sd_mk_431)
## 
##  Mann-Kendall trend test
## 
## data:  summer_standard_dev_all_431$sd_2
## z = -2.4573, n = 29, p-value = 0.014
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
## -132.0000000 2842.0000000   -0.3251232
summer_sd_sens_431 <- sens.slope(summer_standard_dev_all_431$sd_2)
print(summer_sd_sens_431)
## 
##  Sen's slope
## 
## data:  summer_standard_dev_all_431$sd_2
## z = -2.4573, n = 29, p-value = 0.014
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.040118705 -0.006476229
## sample estimates:
## Sen's slope 
## -0.02200642

Winter

winter_standard_dev_all_431 <- standard_dev_431 %>%
  filter(waterDay >= 32 & waterDay <= 182) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())

winter_standard_dev_all_431 <- winter_standard_dev_all_431 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

winter_standard_dev_all_431 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1989 5.673812
1993 3.905666
1995 4.261318
1996 4.685947
1997 5.018318
1998 4.275724
1999 4.204865
2000 4.747757
2001 4.059343
2002 5.261975
2003 3.868759
2004 5.498182
2005 3.823009
2006 4.518857
2007 5.221438
2008 5.738927
2009 4.351076
2010 4.383378
2011 5.344746
2012 4.195307
2013 5.798016
2015 4.763366
2016 4.785310
2017 4.944345
2018 4.038014
2019 3.978953
2020 4.363272
2021 4.251794
2022 4.600003
ggplot(winter_standard_dev_all_431, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 431 average winter temperatures for water years 2005-2021

winter MK & SS for 431 (non-corrected)

winter_sd_mk_431 <- mk.test(winter_standard_dev_all_431$sd_2)
print(winter_sd_mk_431)
## 
##  Mann-Kendall trend test
## 
## data:  winter_standard_dev_all_431$sd_2
## z = -0.018758, n = 29, p-value = 0.985
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##             S          varS           tau 
## -2.000000e+00  2.842000e+03 -4.926108e-03
winter_sd_sens_431 <- sens.slope(winter_standard_dev_all_431$sd_2)
print(winter_sd_sens_431)
## 
##  Sen's slope
## 
## data:  winter_standard_dev_all_431$sd_2
## z = -0.018758, n = 29, p-value = 0.985
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.03484063  0.02642360
## sample estimates:
##   Sen's slope 
## -0.0005581102

Summer and Winter SD CORRECTED

Summer

summer_standard_dev_all_431_ad <- standard_dev_431_ad %>% 
  filter(waterDay >= 244 & waterDay <= 335) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())
summer_standard_dev_all_431_ad <- summer_standard_dev_all_431_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
summer_standard_dev_all_431_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1989 2.470381
1993 2.476121
1995 2.662238
1996 1.796567
1997 2.096373
1998 2.397172
1999 2.152429
2000 1.671520
2001 2.025994
2002 1.892540
2003 2.663410
2004 2.000228
2005 2.559237
2006 1.782485
2007 2.416990
2008 2.188493
2009 2.460397
2010 2.037137
2011 1.863873
2012 1.668191
2013 1.649971
2015 1.952937
2016 2.511608
2017 1.796713
2018 1.792051
2019 2.342931
2020 2.092982
2021 2.286197
2022 2.146296
ggplot(summer_standard_dev_all_431_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 431 average summer temperatures for water years 1986-2021

summer MK & SS 431 (corrected)

summer_sd_mk_431_ad <- mk.test(summer_standard_dev_all_431_ad$sd_2)
print(summer_sd_mk_431_ad)
## 
##  Mann-Kendall trend test
## 
## data:  summer_standard_dev_all_431_ad$sd_2
## z = -1.2568, n = 29, p-value = 0.2088
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
##  -68.0000000 2842.0000000   -0.1674877
summer_sd_sens_431_ad <- sens.slope(summer_standard_dev_all_431_ad$sd_2)
print(summer_sd_sens_431_ad)
## 
##  Sen's slope
## 
## data:  summer_standard_dev_all_431_ad$sd_2
## z = -1.2568, n = 29, p-value = 0.2088
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.025484968  0.006205015
## sample estimates:
## Sen's slope 
## -0.01018055

Winter

winter_standard_dev_all_431_ad <- standard_dev_431_ad %>% 
  filter(waterDay >= 32 & waterDay <= 182) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())
winter_standard_dev_all_431_ad <- winter_standard_dev_all_431_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
winter_standard_dev_all_431_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1989 5.465125
1993 3.755367
1995 4.083871
1996 4.496507
1997 4.817101
1998 4.097535
1999 4.014583
2000 4.525336
2001 3.907362
2002 5.029857
2003 3.707729
2004 5.281855
2005 3.684290
2006 4.518705
2007 5.221173
2008 5.738715
2009 4.351342
2010 4.383690
2011 5.344536
2012 4.195071
2013 5.798491
2015 4.763594
2016 4.785655
2017 4.943994
2018 4.038118
2019 3.978630
2020 4.363460
2021 4.251814
2022 4.600094
ggplot(winter_standard_dev_all_431_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 431 average winter temperatures for water years 1986-2021

winter MK & SS 431 (corrected)

winter_sd_mk_431_ad <- mk.test(winter_standard_dev_all_431_ad$sd_2)
print(winter_sd_mk_431_ad)
## 
##  Mann-Kendall trend test
## 
## data:  winter_standard_dev_all_431_ad$sd_2
## z = 0.46895, n = 29, p-value = 0.6391
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
## 2.600000e+01 2.842000e+03 6.403941e-02
winter_sd_sens_431_ad <- sens.slope(winter_standard_dev_all_431_ad$sd_2)
print(winter_sd_sens_431_ad)
## 
##  Sen's slope
## 
## data:  winter_standard_dev_all_431_ad$sd_2
## z = 0.46895, n = 29, p-value = 0.6391
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.02353986  0.03508924
## sample estimates:
## Sen's slope 
## 0.007052816

Idarado 538

Morrisey 7/12/2004

snotel_538 <- SNOTEL_san_juan_Area %>% 
  filter(site_id == "538")
#str(snotel_538) # check the date, usually a character.  

snotel_538$Date <- as.Date(snotel_538$date) #change date from character to date format, capitalize to work with Water year functon from NWIS.

#THIS WILL CHANGE FOR EACH STATION
snotel_538_clean <- snotel_538 %>% # filter for the timeframe
  filter(Date >= "1979-10-01" & Date <= "2022-09-30") %>%
  #filter(temperature_mean >= -30 & temperature_mean <= 20) %>% # removing outliers   
  addWaterYear() %>% 
  mutate(daymonth = format(as.Date(Date), "%d-%m")) %>% 
  na.omit()

#adding water day using difftime (SUPER COOL. example from [this](https://stackoverflow.com/questions/48123049/create-day-index-based-on-water-year))

snotel_538_clean <- snotel_538_clean %>% 
  group_by(waterYear)%>% 
  mutate(waterDay = (as.integer(difftime(Date, ymd(paste0(waterYear - 1 ,'-09-30')), units = "days"))))
# Check for outliers

ggplot(snotel_538_clean, aes(x = Date, y = temperature_mean)) +
  geom_point() + #lwd = 2) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature (°C)') + 
  xlab('Date')

snotel_538_clean <- snotel_538_clean %>% 
  mutate(temp_diff = abs(temperature_min - temperature_max)) %>% 
  filter(temperature_mean > -40)# %>% 
  #filter(temperature_min < 25)
ggplot(snotel_538_clean, aes(x = Date, y = temp_diff)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  #geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature varience (°C)') + 
  xlab('Date')

Per Steven’s advice: :If there are more than 15 missing days, then…remove that year”

# filtering for temperature anomalies
snotel_538_cull_count <- snotel_538_clean %>% 
  filter(temperature_min > -40) %>% 
  count(waterYear)

snotel_538_cull_count
## # A tibble: 36 x 2
## # Groups:   waterYear [36]
##    waterYear     n
##        <dbl> <int>
##  1      1987   355
##  2      1988   366
##  3      1989   364
##  4      1990   365
##  5      1991   337
##  6      1992   358
##  7      1993   346
##  8      1994   351
##  9      1995   364
## 10      1996   365
## # ... with 26 more rows
# filtering for too few observations in a year
snotel_538_cull_count_days <- snotel_538_clean %>% 
  group_by(waterYear) %>% 
  count(waterYear) %>% 
  filter(n < 350)

snotel_538_cull_count_days
## # A tibble: 2 x 2
## # Groups:   waterYear [2]
##   waterYear     n
##       <dbl> <int>
## 1      1991   337
## 2      1993   346

1991 and 1993 need to be culled.

snotel_538_clean_culled <- snotel_538_clean %>% 
  filter(waterYear != "1991" & waterYear != "1993") # & waterYear != "1990" & waterYear != "1991" & waterYear != "1992" & waterYear != "1994" & waterYear != "2014") #%>% 
  #filter(temperature_mean > -49)
ggplot(snotel_538_clean_culled, aes(x = Date, y = temperature_mean)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature (°C)') + 
  xlab('Date')

temp_538_xts <- xts(snotel_538_clean_culled$temperature_mean, order.by = snotel_538_clean_culled$Date)

dygraph(temp_538_xts) %>%
  dyAxis("y", label = "Daily mean temperature (°C)") 
snotel_538_clean_culled <- snotel_538_clean_culled %>% 
  filter(temperature_mean < 20)

temp_538_xts <- xts(snotel_538_clean_culled$temperature_mean, order.by = snotel_538_clean_culled$Date)

dygraph(temp_538_xts) %>%
  dyAxis("y", label = "Daily mean temperature (°C)") 

Idarado 538

Morrisey 7/12/2004

snotel_538_adjusted <- snotel_538_clean_culled %>%
  mutate(temp_ad = if_else(Date < "2004-07-12", ((5.3*10^(-7))*(temperature_mean^(4))+(3.72*10^(-5))*(temperature_mean^(3))-(2.16*10^(-3))*(temperature_mean^(2))-(7.32*10^(-2))*(temperature_mean)+1.37)+temperature_mean, temperature_mean))

Non-corrected

538 Detrended

#using the clean culled df:

#average water year temperature

yearly_wy_aver_538 <- snotel_538_adjusted %>% 
  group_by(waterYear) %>% 
  mutate(aver_ann_temp = mean(temperature_mean))

#Average temperature by day for all water years:

daily_wy_aver_538 <- yearly_wy_aver_538 %>% 
  group_by(daymonth) %>% 
  mutate(aver_day_temp = mean(aver_ann_temp))

#average mean temperature by day for the period of record:

daily_wy_aver_538 <- daily_wy_aver_538 %>% 
  group_by(daymonth) %>% 
  mutate(all_ave_temp = mean(daily_wy_aver_538$aver_day_temp))

# try to show all years as means. 
daily_wy_aver2_538 <-daily_wy_aver_538 %>% 
  group_by(waterDay) %>%
  mutate(date_temp = mean(temperature_mean))
  

daily_wy_aver2_538$date_temp <- signif(daily_wy_aver2_538$date_temp,3) #reduce the sig figs


ggplot(daily_wy_aver2_538, aes(x = waterDay, y = date_temp))+
  geom_line(size= 0.7) +
  theme_few() +
  ylab('Average Daily temperature (°C)') + 
  xlab('Day of water year')

538 SD

standard_dev_538 <- daily_wy_aver_538 %>% 
  group_by(waterYear) %>% 
  mutate(residual = (all_ave_temp-aver_ann_temp)+temperature_mean-aver_day_temp) %>% 
  mutate(deviation = abs(residual-lag(residual)))

standard_dev_all_538 <- standard_dev_538 %>% 
  group_by(waterYear) %>% 
  mutate(nmbr = n())

standard_dev_all_538 <- standard_dev_all_538 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

standard_dev_all_538 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 8.006364
1988 8.601864
1989 8.632529
1990 8.392986
1992 7.829013
1994 10.169815
1995 7.658097
1996 7.925011
1997 7.934668
1998 8.455869
1999 7.252626
2000 7.992580
2001 8.428201
2002 8.883209
2003 8.309823
2004 8.066471
2005 7.228205
2006 7.518896
2007 7.890984
2008 8.117130
2009 7.048527
2010 8.062733
2011 7.859864
2012 7.787906
2013 8.410412
2014 7.465408
2015 6.906228
2016 7.611126
2017 7.235848
2018 7.239780
2019 7.718304
2020 7.976082
2021 8.027478
2022 7.664387
ggplot(standard_dev_all_538, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 538 average temperatures for water years 2005-2021

MK & SS for 538 (non-corrected)

sd_mk_538 <- mk.test(standard_dev_all_538$sd_2)
print(sd_mk_538)
## 
##  Mann-Kendall trend test
## 
## data:  standard_dev_all_538$sd_2
## z = -2.6684, n = 34, p-value = 0.007621
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
## -181.0000000 4550.3333333   -0.3226381
sd_sens_538 <- sens.slope(standard_dev_all_538$sd_2)
print(sd_sens_538)
## 
##  Sen's slope
## 
## data:  standard_dev_all_538$sd_2
## z = -2.6684, n = 34, p-value = 0.007621
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.042981556 -0.006896251
## sample estimates:
## Sen's slope 
## -0.02384088

Corrected

538 Detrended (corrected)

#using the clean culled df:
#average water year temperature
yearly_wy_aver_538_ad <- snotel_538_adjusted %>% 
  group_by(waterYear) %>% 
  mutate(aver_ann_temp_ad = mean(temp_ad))
#Average temperature by day for all water years:
daily_wy_aver_538_ad <- yearly_wy_aver_538_ad %>% 
  group_by(daymonth) %>% 
  mutate(aver_day_temp_ad = mean(aver_ann_temp_ad))
#average mean temperature by day for the period of record:
daily_wy_aver_538_ad <- daily_wy_aver_538_ad %>% 
  group_by(daymonth) %>% 
  mutate(all_ave_temp_ad = mean(daily_wy_aver_538_ad$aver_day_temp_ad))
# try to show all years as means. 
daily_wy_aver2_538_ad <-daily_wy_aver_538_ad %>% 
  group_by(waterDay) %>%
  mutate(date_temp_ad = mean(temp_ad))
  
daily_wy_aver2_538_ad$date_temp_ad <- signif(daily_wy_aver2_538_ad$date_temp_ad,3) #reduce the sig figs
ggplot(daily_wy_aver2_538_ad, aes(x = waterDay, y = date_temp_ad))+
  geom_line(size= 0.7) +
  theme_few() +
  ylab('Average Daily temperature (°C)') + 
  xlab('Day of water year')

538 SS (corrected)

standard_dev_538_ad <- daily_wy_aver_538_ad %>% 
  group_by(waterYear) %>% 
  #filter(waterYear >= 1987 & waterYear <= 2021) %>% 
  mutate(residual = (all_ave_temp_ad-aver_ann_temp_ad)+temp_ad-aver_day_temp_ad) %>% 
  mutate(deviation = abs(residual-lag(residual)))
standard_dev_all_538_ad <- standard_dev_538_ad %>% 
  group_by(waterYear) %>% 
  mutate(nmbr = n())
standard_dev_all_538_ad <- standard_dev_all_538_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
standard_dev_all_538_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 7.447584
1988 8.019241
1989 8.069873
1990 7.812557
1992 7.272544
1994 9.549437
1995 7.113641
1996 7.353811
1997 7.396458
1998 7.860736
1999 6.739299
2000 7.408528
2001 7.835100
2002 8.260161
2003 7.706375
2004 7.450714
2005 7.228187
2006 7.519259
2007 7.891254
2008 8.117349
2009 7.048722
2010 8.062844
2011 7.860215
2012 7.788573
2013 8.411054
2014 7.465688
2015 6.906947
2016 7.611742
2017 7.235817
2018 7.240024
2019 7.718445
2020 7.976612
2021 8.027693
2022 7.664991
ggplot(standard_dev_all_538_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 538 average temperatures for water years 1986-2021

MK & SS 538 (corrected)

sd_mk_538_ad <- mk.test(standard_dev_all_538_ad$sd_2)
print(sd_mk_538_ad)
## 
##  Mann-Kendall trend test
## 
## data:  standard_dev_all_538_ad$sd_2
## z = 0.14824, n = 34, p-value = 0.8821
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
## 1.100000e+01 4.550333e+03 1.960784e-02
sd_sens_538_ad <- sens.slope(standard_dev_all_538_ad$sd_2)
print(sd_sens_538_ad)
## 
##  Sen's slope
## 
## data:  standard_dev_all_538_ad$sd_2
## z = 0.14824, n = 34, p-value = 0.8821
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.01508416  0.01703536
## sample estimates:
##  Sen's slope 
## 0.0009105465

Summer and Winter SD NOT-CORRECTED

Summer

summer_standard_dev_all_538 <- standard_dev_538 %>%
  filter(waterDay >= 244 & waterDay <= 335) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())

summer_standard_dev_all_538 <- summer_standard_dev_all_538 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

summer_standard_dev_all_538 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 2.286005
1988 2.273513
1989 3.233122
1990 2.664477
1992 2.736380
1994 1.935542
1995 3.113265
1996 1.898021
1997 2.007429
1998 2.790447
1999 2.372218
2000 1.860330
2001 2.400933
2002 2.466890
2003 2.718293
2004 2.323215
2005 2.806529
2006 1.918794
2007 2.183218
2008 2.300074
2009 2.431260
2010 2.191297
2011 1.862060
2012 1.694107
2013 2.005642
2014 1.961264
2015 2.139016
2016 2.301314
2017 1.746921
2018 1.640625
2019 2.266448
2020 2.256431
2021 2.070257
2022 1.944235
ggplot(summer_standard_dev_all_538, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 538 average summer temperatures for water years 2005-2021

summer MK & SS for 538 (non-corrected)

summer_sd_mk_538 <- mk.test(summer_standard_dev_all_538$sd_2)
print(summer_sd_mk_538)
## 
##  Mann-Kendall trend test
## 
## data:  summer_standard_dev_all_538$sd_2
## z = -2.698, n = 34, p-value = 0.006975
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
## -183.0000000 4550.3333333   -0.3262032
summer_sd_sens_538 <- sens.slope(summer_standard_dev_all_538$sd_2)
print(summer_sd_sens_538)
## 
##  Sen's slope
## 
## data:  summer_standard_dev_all_538$sd_2
## z = -2.698, n = 34, p-value = 0.006975
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.033679036 -0.005513698
## sample estimates:
## Sen's slope 
## -0.01860794

Winter

winter_standard_dev_all_538 <- standard_dev_538 %>%
  filter(waterDay >= 32 & waterDay <= 182) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())

winter_standard_dev_all_538 <- winter_standard_dev_all_538 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

winter_standard_dev_all_538 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 4.261954
1988 5.174663
1989 6.182112
1990 5.129029
1992 4.206723
1994 5.619481
1995 4.701116
1996 4.620550
1997 5.021863
1998 4.465685
1999 4.642997
2000 4.872329
2001 4.227546
2002 5.161789
2003 4.232413
2004 5.464700
2005 4.007572
2006 4.830993
2007 5.347082
2008 5.517212
2009 4.330423
2010 4.438182
2011 5.393996
2012 4.319554
2013 5.628011
2014 4.325964
2015 4.611312
2016 4.735485
2017 5.218358
2018 4.425586
2019 4.275462
2020 4.587430
2021 4.421560
2022 4.624955
ggplot(winter_standard_dev_all_538, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 538 average winter temperatures for water years 2005-2021

winter MK & SS for 538 (non-corrected)

winter_sd_mk_538 <- mk.test(winter_standard_dev_all_538$sd_2)
print(winter_sd_mk_538)
## 
##  Mann-Kendall trend test
## 
## data:  winter_standard_dev_all_538$sd_2
## z = -0.80052, n = 34, p-value = 0.4234
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##             S          varS           tau 
##  -55.00000000 4550.33333333   -0.09803922
winter_sd_sens_538 <- sens.slope(winter_standard_dev_all_538$sd_2)
print(winter_sd_sens_538)
## 
##  Sen's slope
## 
## data:  winter_standard_dev_all_538$sd_2
## z = -0.80052, n = 34, p-value = 0.4234
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.026752749  0.009824772
## sample estimates:
##  Sen's slope 
## -0.008862001

Summer and Winter SD CORRECTED

Summer

summer_standard_dev_all_538_ad <- standard_dev_538_ad %>% 
  filter(waterDay >= 244 & waterDay <= 335) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())
summer_standard_dev_all_538_ad <- summer_standard_dev_all_538_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
summer_standard_dev_all_538_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 2.055839
1988 2.049493
1989 2.902440
1990 2.396673
1992 2.463107
1994 1.734275
1995 2.798052
1996 1.701215
1997 1.804148
1998 2.508680
1999 2.138026
2000 1.664807
2001 2.163899
2002 2.210401
2003 2.435307
2004 2.160957
2005 2.805521
2006 1.919561
2007 2.184205
2008 2.299542
2009 2.430957
2010 2.190804
2011 1.862764
2012 1.698114
2013 2.007185
2014 1.961864
2015 2.142038
2016 2.302889
2017 1.748470
2018 1.641454
2019 2.263670
2020 2.256562
2021 2.068673
2022 1.943594
ggplot(summer_standard_dev_all_538_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 538 average summer temperatures for water years 1986-2021

summer MK & SS 538 (corrected)

summer_sd_mk_538_ad <- mk.test(summer_standard_dev_all_538_ad$sd_2)
print(summer_sd_mk_538_ad)
## 
##  Mann-Kendall trend test
## 
## data:  summer_standard_dev_all_538_ad$sd_2
## z = -1.186, n = 34, p-value = 0.2356
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##           S        varS         tau 
##  -81.000000 4550.333333   -0.144385
summer_sd_sens_538_ad <- sens.slope(summer_standard_dev_all_538_ad$sd_2)
print(summer_sd_sens_538_ad)
## 
##  Sen's slope
## 
## data:  summer_standard_dev_all_538_ad$sd_2
## z = -1.186, n = 34, p-value = 0.2356
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.020711408  0.005565886
## sample estimates:
## Sen's slope 
## -0.00764982

Winter

winter_standard_dev_all_538_ad <- standard_dev_538_ad %>% 
  filter(waterDay >= 32 & waterDay <= 182) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())
winter_standard_dev_all_538_ad <- winter_standard_dev_all_538_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
winter_standard_dev_all_538_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 4.109307
1988 4.994440
1989 5.973268
1990 4.943886
1992 4.047152
1994 5.487129
1995 4.504527
1996 4.445704
1997 4.843480
1998 4.304597
1999 4.457758
2000 4.678027
2001 4.085048
2002 4.972893
2003 4.083408
2004 5.267994
2005 4.007627
2006 4.830574
2007 5.346512
2008 5.515768
2009 4.329745
2010 4.437191
2011 5.393419
2012 4.319038
2013 5.628675
2014 4.325676
2015 4.611280
2016 4.734318
2017 5.216960
2018 4.424429
2019 4.274306
2020 4.586853
2021 4.421444
2022 4.625350
ggplot(winter_standard_dev_all_538_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 538 average winter temperatures for water years 1986-2021

winter MK & SS 538 (corrected)

winter_sd_mk_538_ad <- mk.test(winter_standard_dev_all_538_ad$sd_2)
print(winter_sd_mk_538_ad)
## 
##  Mann-Kendall trend test
## 
## data:  winter_standard_dev_all_538_ad$sd_2
## z = -0.26684, n = 34, p-value = 0.7896
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##             S          varS           tau 
##  -19.00000000 4550.33333333   -0.03386809
winter_sd_sens_538_ad <- sens.slope(winter_standard_dev_all_538_ad$sd_2)
print(winter_sd_sens_538_ad)
## 
##  Sen's slope
## 
## data:  winter_standard_dev_all_538_ad$sd_2
## z = -0.26684, n = 34, p-value = 0.7896
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.01995480  0.01766204
## sample estimates:
##  Sen's slope 
## -0.001595321

Lone Cone 589

Oyler -> Morrisey 6/22/2005

snotel_589 <- SNOTEL_san_juan_Area %>% 
  filter(site_id == "589")
#str(snotel_589) # check the date, usually a character.  

snotel_589$Date <- as.Date(snotel_589$date) #change date from character to date format, capitalize to work with Water year functon from NWIS.

#THIS WILL CHANGE FOR EACH STATION
snotel_589_clean <- snotel_589 %>% # filter for the timeframe
  filter(Date >= "1979-10-01" & Date <= "2022-09-30") %>%
  #filter(temperature_mean >= -30 & temperature_mean <= 20) %>% # removing outliers   
  addWaterYear() %>% 
  mutate(daymonth = format(as.Date(Date), "%d-%m")) %>% 
  na.omit()

#adding water day using difftime (SUPER COOL. example from [this](https://stackoverflow.com/questions/48123049/create-day-index-based-on-water-year))

snotel_589_clean <- snotel_589_clean %>% 
  group_by(waterYear)%>% 
  mutate(waterDay = (as.integer(difftime(Date, ymd(paste0(waterYear - 1 ,'-09-30')), units = "days"))))
# Check for outliers

ggplot(snotel_589_clean, aes(x = Date, y = temperature_mean)) +
  geom_point() + #lwd = 2) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature (°C)') + 
  xlab('Date')

snotel_589_clean <- snotel_589_clean %>% 
  mutate(temp_diff = abs(temperature_min - temperature_max)) %>% 
  filter(temperature_mean > -40)# %>% 
  #filter(temperature_min < 25)
ggplot(snotel_589_clean, aes(x = Date, y = temp_diff)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  #geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature varience (°C)') + 
  xlab('Date')

Per Steven’s advice: :If there are more than 15 missing days, then…remove that year”

# filtering for temperature anomalies
snotel_589_cull_count <- snotel_589_clean %>% 
  filter(temperature_min > -40) %>% 
  count(waterYear)

snotel_589_cull_count
## # A tibble: 37 x 2
## # Groups:   waterYear [37]
##    waterYear     n
##        <dbl> <int>
##  1      1986     1
##  2      1987   358
##  3      1988   366
##  4      1989   362
##  5      1990   365
##  6      1991   359
##  7      1992   366
##  8      1993   350
##  9      1994   337
## 10      1995   364
## # ... with 27 more rows
# filtering for too few observations in a year
snotel_589_cull_count_days <- snotel_589_clean %>% 
  group_by(waterYear) %>% 
  count(waterYear) %>% 
  filter(n < 350)

snotel_589_cull_count_days
## # A tibble: 10 x 2
## # Groups:   waterYear [10]
##    waterYear     n
##        <dbl> <int>
##  1      1986     1
##  2      1994   337
##  3      1997   346
##  4      2006   317
##  5      2009   314
##  6      2010   332
##  7      2011   309
##  8      2012   322
##  9      2013   331
## 10      2014   335

1986, 1994, 1997, 2006, 2009, 2010, 2011, 2012, 2013, 2014 need to be culled.

snotel_589_clean_culled <- snotel_589_clean %>% 
  filter(waterYear != "1986" & waterYear != "1994" & waterYear != "1997" & waterYear != "2006" & waterYear != "2009" & waterYear != "2010" & waterYear != "2011" & waterYear != "2012" & waterYear != "2013" & waterYear != "2014") #%>% 
  #filter(temperature_mean > -49)
ggplot(snotel_589_clean_culled, aes(x = Date, y = temperature_mean)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature (°C)') + 
  xlab('Date')

temp_589_xts <- xts(snotel_589_clean_culled$temperature_mean, order.by = snotel_589_clean_culled$Date)

dygraph(temp_589_xts) %>%
  dyAxis("y", label = "Daily mean temperature (°C)") 
snotel_589_clean_culled <- snotel_589_clean_culled %>% 
  filter(temperature_mean < 20)

temp_589_xts <- xts(snotel_589_clean_culled$temperature_mean, order.by = snotel_589_clean_culled$Date)

dygraph(temp_589_xts) %>%
  dyAxis("y", label = "Daily mean temperature (°C)") 

Lone Cone 589

Oyler -> Morrisey 6/22/2005

snotel_589_adjusted <- snotel_589_clean_culled %>%
  mutate(temp_ad = if_else(Date < "2005-06-22", ((5.3*10^(-7))*(temperature_mean^(4))+(3.72*10^(-5))*(temperature_mean^(3))-(2.16*10^(-3))*(temperature_mean^(2))-(7.32*10^(-2))*(temperature_mean)+1.37)+temperature_mean, temperature_mean))

Non-corrected

589 Detrended

#using the clean culled df:

#average water year temperature

yearly_wy_aver_589 <- snotel_589_adjusted %>% 
  group_by(waterYear) %>% 
  mutate(aver_ann_temp = mean(temperature_mean))

#Average temperature by day for all water years:

daily_wy_aver_589 <- yearly_wy_aver_589 %>% 
  group_by(daymonth) %>% 
  mutate(aver_day_temp = mean(aver_ann_temp))

#average mean temperature by day for the period of record:

daily_wy_aver_589 <- daily_wy_aver_589 %>% 
  group_by(daymonth) %>% 
  mutate(all_ave_temp = mean(daily_wy_aver_589$aver_day_temp))

# try to show all years as means. 
daily_wy_aver2_589 <-daily_wy_aver_589 %>% 
  group_by(waterDay) %>%
  mutate(date_temp = mean(temperature_mean))
  

daily_wy_aver2_589$date_temp <- signif(daily_wy_aver2_589$date_temp,3) #reduce the sig figs


ggplot(daily_wy_aver2_589, aes(x = waterDay, y = date_temp))+
  geom_line(size= 0.7) +
  theme_few() +
  ylab('Average Daily temperature (°C)') + 
  xlab('Day of water year')

589 SD

standard_dev_589 <- daily_wy_aver_589 %>% 
  group_by(waterYear) %>% 
  mutate(residual = (all_ave_temp-aver_ann_temp)+temperature_mean-aver_day_temp) %>% 
  mutate(deviation = abs(residual-lag(residual)))

standard_dev_all_589 <- standard_dev_589 %>% 
  group_by(waterYear) %>% 
  mutate(nmbr = n())

standard_dev_all_589 <- standard_dev_all_589 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

standard_dev_all_589 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 8.224944
1988 8.773281
1989 8.776693
1990 8.580289
1991 8.502502
1992 7.975611
1993 8.199081
1995 7.946318
1996 8.231469
1998 8.706109
1999 7.431979
2000 8.232988
2001 8.771085
2002 9.082003
2003 8.732769
2004 8.380419
2005 8.458900
2007 8.212611
2008 8.408220
2015 7.103666
2016 7.842413
2017 7.527960
2018 7.506884
2019 8.029199
2020 8.271099
2021 8.278625
2022 8.020852
ggplot(standard_dev_all_589, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 589 average temperatures for water years 2005-2021

MK & SS for 589 (non-corrected)

sd_mk_589 <- mk.test(standard_dev_all_589$sd_2)
print(sd_mk_589)
## 
##  Mann-Kendall trend test
## 
## data:  standard_dev_all_589$sd_2
## z = -1.8345, n = 27, p-value = 0.06658
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
##  -89.0000000 2301.0000000   -0.2535613
sd_sens_589 <- sens.slope(standard_dev_all_589$sd_2)
print(sd_sens_589)
## 
##  Sen's slope
## 
## data:  standard_dev_all_589$sd_2
## z = -1.8345, n = 27, p-value = 0.06658
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.043902404  0.001230005
## sample estimates:
## Sen's slope 
## -0.02183399

Corrected

589 Detrended (corrected)

#using the clean culled df:
#average water year temperature
yearly_wy_aver_589_ad <- snotel_589_adjusted %>% 
  group_by(waterYear) %>% 
  mutate(aver_ann_temp_ad = mean(temp_ad))
#Average temperature by day for all water years:
daily_wy_aver_589_ad <- yearly_wy_aver_589_ad %>% 
  group_by(daymonth) %>% 
  mutate(aver_day_temp_ad = mean(aver_ann_temp_ad))
#average mean temperature by day for the period of record:
daily_wy_aver_589_ad <- daily_wy_aver_589_ad %>% 
  group_by(daymonth) %>% 
  mutate(all_ave_temp_ad = mean(daily_wy_aver_589_ad$aver_day_temp_ad))
# try to show all years as means. 
daily_wy_aver2_589_ad <-daily_wy_aver_589_ad %>% 
  group_by(waterDay) %>%
  mutate(date_temp_ad = mean(temp_ad))
  
daily_wy_aver2_589_ad$date_temp_ad <- signif(daily_wy_aver2_589_ad$date_temp_ad,3) #reduce the sig figs
ggplot(daily_wy_aver2_589_ad, aes(x = waterDay, y = date_temp_ad))+
  geom_line(size= 0.7) +
  theme_few() +
  ylab('Average Daily temperature (°C)') + 
  xlab('Day of water year')

589 SS (corrected)

standard_dev_589_ad <- daily_wy_aver_589_ad %>% 
  group_by(waterYear) %>% 
  #filter(waterYear >= 1987 & waterYear <= 2021) %>% 
  mutate(residual = (all_ave_temp_ad-aver_ann_temp_ad)+temp_ad-aver_day_temp_ad) %>% 
  mutate(deviation = abs(residual-lag(residual)))
standard_dev_all_589_ad <- standard_dev_589_ad %>% 
  group_by(waterYear) %>% 
  mutate(nmbr = n())
standard_dev_all_589_ad <- standard_dev_all_589_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
standard_dev_all_589_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 7.671688
1988 8.200568
1989 8.222864
1990 7.999469
1991 7.962227
1992 7.420415
1993 7.639311
1995 7.378994
1996 7.641722
1998 8.086353
1999 6.914950
2000 7.633161
2001 8.156564
2002 8.440080
2003 8.103839
2004 7.827141
2005 7.781158
2007 8.212720
2008 8.408841
2015 7.104293
2016 7.842763
2017 7.527954
2018 7.507329
2019 8.029667
2020 8.271310
2021 8.278726
2022 8.021229
ggplot(standard_dev_all_589_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 589 average temperatures for water years 1986-2021

MK & SS 589 (corrected)

sd_mk_589_ad <- mk.test(standard_dev_all_589_ad$sd_2)
print(sd_mk_589_ad)
## 
##  Mann-Kendall trend test
## 
## data:  standard_dev_all_589_ad$sd_2
## z = 0.6671, n = 27, p-value = 0.5047
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
## 3.300000e+01 2.301000e+03 9.401709e-02
sd_sens_589_ad <- sens.slope(standard_dev_all_589_ad$sd_2)
print(sd_sens_589_ad)
## 
##  Sen's slope
## 
## data:  standard_dev_all_589_ad$sd_2
## z = 0.6671, n = 27, p-value = 0.5047
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.01153608  0.02498422
## sample estimates:
## Sen's slope 
## 0.008250776

Summer and Winter SD NOT-CORRECTED

Summer

summer_standard_dev_all_589 <- standard_dev_589 %>%
  filter(waterDay >= 244 & waterDay <= 335) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())

summer_standard_dev_all_589 <- summer_standard_dev_all_589 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

summer_standard_dev_all_589 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 1.967742
1988 2.534636
1989 3.461696
1990 2.403263
1991 2.525811
1992 2.623689
1993 3.041407
1995 3.367172
1996 2.111391
1998 2.893363
1999 2.622980
2000 1.957874
2001 2.664325
2002 2.647149
2003 2.840568
2004 2.383584
2005 3.230216
2007 2.297878
2008 2.318882
2015 2.142048
2016 2.374472
2017 1.826211
2018 1.691936
2019 2.406265
2020 2.368206
2021 2.273992
2022 2.104514
ggplot(summer_standard_dev_all_589, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 589 average summer temperatures for water years 2005-2021

summer MK & SS for 589 (non-corrected)

summer_sd_mk_589 <- mk.test(summer_standard_dev_all_589$sd_2)
print(summer_sd_mk_589)
## 
##  Mann-Kendall trend test
## 
## data:  summer_standard_dev_all_589$sd_2
## z = -2.2515, n = 27, p-value = 0.02436
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
## -109.0000000 2301.0000000   -0.3105413
summer_sd_sens_589 <- sens.slope(summer_standard_dev_all_589$sd_2)
print(summer_sd_sens_589)
## 
##  Sen's slope
## 
## data:  summer_standard_dev_all_589$sd_2
## z = -2.2515, n = 27, p-value = 0.02436
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.049347872 -0.001693607
## sample estimates:
## Sen's slope 
## -0.02326586

Winter

winter_standard_dev_all_589 <- standard_dev_589 %>%
  filter(waterDay >= 32 & waterDay <= 182) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())

winter_standard_dev_all_589 <- winter_standard_dev_all_589 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

winter_standard_dev_all_589 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 4.353379
1988 5.361949
1989 6.315764
1990 5.337449
1991 5.180784
1992 4.169408
1993 4.454775
1995 4.870066
1996 4.842621
1998 4.523873
1999 4.971961
2000 5.060819
2001 4.408432
2002 5.356568
2003 4.383529
2004 5.881583
2005 4.319148
2007 5.569424
2008 5.616044
2015 4.729116
2016 4.857060
2017 5.445588
2018 4.504100
2019 4.213170
2020 4.673225
2021 4.620711
2022 4.838951
ggplot(winter_standard_dev_all_589, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 589 average winter temperatures for water years 2005-2021

winter MK & SS for 589 (non-corrected)

winter_sd_mk_589 <- mk.test(winter_standard_dev_all_589$sd_2)
print(winter_sd_mk_589)
## 
##  Mann-Kendall trend test
## 
## data:  winter_standard_dev_all_589$sd_2
## z = -0.62541, n = 27, p-value = 0.5317
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##             S          varS           tau 
##  -31.00000000 2301.00000000   -0.08831909
winter_sd_sens_589 <- sens.slope(winter_standard_dev_all_589$sd_2)
print(winter_sd_sens_589)
## 
##  Sen's slope
## 
## data:  winter_standard_dev_all_589$sd_2
## z = -0.62541, n = 27, p-value = 0.5317
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.03759354  0.01968776
## sample estimates:
## Sen's slope 
## -0.01157889

Summer and Winter SD CORRECTED

Summer

summer_standard_dev_all_589_ad <- standard_dev_589_ad %>% 
  filter(waterDay >= 244 & waterDay <= 335) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())
summer_standard_dev_all_589_ad <- summer_standard_dev_all_589_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
summer_standard_dev_all_589_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 1.766231
1988 2.285505
1989 3.125668
1990 2.158914
1991 2.282519
1992 2.358745
1993 2.738134
1995 3.026153
1996 1.891330
1998 2.601086
1999 2.364119
2000 1.751532
2001 2.401863
2002 2.373286
2003 2.546455
2004 2.136727
2005 2.993266
2007 2.299972
2008 2.319300
2015 2.145049
2016 2.375700
2017 1.827998
2018 1.692844
2019 2.406014
2020 2.368208
2021 2.271396
2022 2.103934
ggplot(summer_standard_dev_all_589_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 589 average summer temperatures for water years 1986-2021

summer MK & SS 589 (corrected)

summer_sd_mk_589_ad <- mk.test(summer_standard_dev_all_589_ad$sd_2)
print(summer_sd_mk_589_ad)
## 
##  Mann-Kendall trend test
## 
## data:  summer_standard_dev_all_589_ad$sd_2
## z = -0.83388, n = 27, p-value = 0.4044
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
##  -41.0000000 2301.0000000   -0.1168091
summer_sd_sens_589_ad <- sens.slope(summer_standard_dev_all_589_ad$sd_2)
print(summer_sd_sens_589_ad)
## 
##  Sen's slope
## 
## data:  summer_standard_dev_all_589_ad$sd_2
## z = -0.83388, n = 27, p-value = 0.4044
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.02845926  0.00764659
## sample estimates:
## Sen's slope 
## -0.00560237

Winter

winter_standard_dev_all_589_ad <- standard_dev_589_ad %>% 
  filter(waterDay >= 32 & waterDay <= 182) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())
winter_standard_dev_all_589_ad <- winter_standard_dev_all_589_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
winter_standard_dev_all_589_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 4.215611
1988 5.197066
1989 6.124702
1990 5.163969
1991 5.065782
1992 4.018647
1993 4.309492
1995 4.680477
1996 4.671648
1998 4.353429
1999 4.771281
2000 4.857137
2001 4.272848
2002 5.152772
2003 4.243131
2004 5.666034
2005 4.181523
2007 5.567999
2008 5.616390
2015 4.728585
2016 4.856335
2017 5.443448
2018 4.503520
2019 4.213250
2020 4.672479
2021 4.619604
2022 4.838898
ggplot(winter_standard_dev_all_589_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 589 average winter temperatures for water years 1986-2021

winter MK & SS 589 (corrected)

winter_sd_mk_589_ad <- mk.test(winter_standard_dev_all_589_ad$sd_2)
print(winter_sd_mk_589_ad)
## 
##  Mann-Kendall trend test
## 
## data:  winter_standard_dev_all_589_ad$sd_2
## z = -0.25016, n = 27, p-value = 0.8025
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##             S          varS           tau 
##  -13.00000000 2301.00000000   -0.03703704
winter_sd_sens_589_ad <- sens.slope(winter_standard_dev_all_589_ad$sd_2)
print(winter_sd_sens_589_ad)
## 
##  Sen's slope
## 
## data:  winter_standard_dev_all_589_ad$sd_2
## z = -0.25016, n = 27, p-value = 0.8025
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.03055989  0.02885928
## sample estimates:
##  Sen's slope 
## -0.002906196

Middle Creek 624

Morrisey 6/26/2006

snotel_624 <- SNOTEL_san_juan_Area %>% 
  filter(site_id == "624")
#str(snotel_624) # check the date, usually a character.  

snotel_624$Date <- as.Date(snotel_624$date) #change date from character to date format, capitalize to work with Water year functon from NWIS.

#THIS WILL CHANGE FOR EACH STATION
snotel_624_clean <- snotel_624 %>% # filter for the timeframe
  filter(Date >= "1979-10-01" & Date <= "2022-09-30") %>%
  #filter(temperature_mean >= -30 & temperature_mean <= 20) %>% # removing outliers   
  addWaterYear() %>% 
  mutate(daymonth = format(as.Date(Date), "%d-%m")) %>% 
  na.omit()

#adding water day using difftime (SUPER COOL. example from [this](https://stackoverflow.com/questions/48123049/create-day-index-based-on-water-year))

snotel_624_clean <- snotel_624_clean %>% 
  group_by(waterYear)%>% 
  mutate(waterDay = (as.integer(difftime(Date, ymd(paste0(waterYear - 1 ,'-09-30')), units = "days"))))
# Check for outliers

ggplot(snotel_624_clean, aes(x = Date, y = temperature_mean)) +
  geom_point() + #lwd = 2) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature (°C)') + 
  xlab('Date')

snotel_624_clean <- snotel_624_clean %>% 
  mutate(temp_diff = abs(temperature_min - temperature_max)) %>% 
  filter(temperature_mean > -40) %>% 
  filter(temperature_mean < 25)
ggplot(snotel_624_clean, aes(x = Date, y = temp_diff)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  #geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature varience (°C)') + 
  xlab('Date')

Per Steven’s advice: :If there are more than 15 missing days, then…remove that year”

# filtering for temperature anomalies
snotel_624_cull_count <- snotel_624_clean %>% 
  filter(temperature_min > -40) %>% 
  count(waterYear)

snotel_624_cull_count
## # A tibble: 43 x 2
## # Groups:   waterYear [43]
##    waterYear     n
##        <dbl> <int>
##  1      1980    59
##  2      1981   354
##  3      1982     1
##  4      1983   312
##  5      1984   360
##  6      1985   362
##  7      1986   364
##  8      1987   360
##  9      1988   340
## 10      1989   363
## # ... with 33 more rows
# filtering for too few observations in a year
snotel_624_cull_count_days <- snotel_624_clean %>% 
  group_by(waterYear) %>% 
  count(waterYear) %>% 
  filter(n < 350)

snotel_624_cull_count_days
## # A tibble: 9 x 2
## # Groups:   waterYear [9]
##   waterYear     n
##       <dbl> <int>
## 1      1980    59
## 2      1982     1
## 3      1983   312
## 4      1988   340
## 5      1991   338
## 6      1993   330
## 7      1994   330
## 8      2021   338
## 9      2022   348

1980, 1982, 1983, 1988, 1991, 1993, 1994, 2021, 2022 need to be culled.

snotel_624_clean_culled <- snotel_624_clean %>% 
  filter(waterYear != "1980" & waterYear != "1981" & waterYear != "1982" & waterYear != "1983" & waterYear != "1988" & waterYear != "1991" & waterYear != "1993" & waterYear != "1994" & waterYear != "2021" & waterYear != "2022")# & waterYear != "2014") #%>% 
  #filter(temperature_mean > -49)

ggplot(snotel_624_clean_culled, aes(x = Date, y = temp_diff)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  #geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature varience (°C)') + 
  xlab('Date')

ggplot(snotel_624_clean_culled, aes(x = Date, y = temperature_mean)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature (°C)') + 
  xlab('Date')

temp_624_xts <- xts(snotel_624_clean_culled$temperature_mean, order.by = snotel_624_clean_culled$Date)

dygraph(temp_624_xts) %>%
  dyAxis("y", label = "Daily mean temperature (°C)") 
snotel_624_clean_culled <- snotel_624_clean_culled %>% 
  filter(temperature_mean < 20)

temp_624_xts <- xts(snotel_624_clean_culled$temperature_mean, order.by = snotel_624_clean_culled$Date)

dygraph(temp_624_xts) %>%
  dyAxis("y", label = "Daily mean temperature (°C)") 

Middle Creek 624 Morrisey 6/26/2006

snotel_624_adjusted <- snotel_624_clean_culled %>%
  mutate(temp_ad = if_else(Date < "2006-06-26", ((5.3*10^(-7))*(temperature_mean^(4))+(3.72*10^(-5))*(temperature_mean^(3))-(2.16*10^(-3))*(temperature_mean^(2))-(7.32*10^(-2))*(temperature_mean)+1.37)+temperature_mean, temperature_mean))

Non-corrected

624 Detrended

#using the clean culled df:

#average water year temperature

yearly_wy_aver_624 <- snotel_624_adjusted %>% 
  group_by(waterYear) %>% 
  mutate(aver_ann_temp = mean(temperature_mean))

#Average temperature by day for all water years:

daily_wy_aver_624 <- yearly_wy_aver_624 %>% 
  group_by(daymonth) %>% 
  mutate(aver_day_temp = mean(aver_ann_temp))

#average mean temperature by day for the period of record:

daily_wy_aver_624 <- daily_wy_aver_624 %>% 
  group_by(daymonth) %>% 
  mutate(all_ave_temp = mean(daily_wy_aver_624$aver_day_temp))

# try to show all years as means. 
daily_wy_aver2_624 <-daily_wy_aver_624 %>% 
  group_by(waterDay) %>%
  mutate(date_temp = mean(temperature_mean))
  

daily_wy_aver2_624$date_temp <- signif(daily_wy_aver2_624$date_temp,3) #reduce the sig figs


ggplot(daily_wy_aver2_624, aes(x = waterDay, y = date_temp))+
  geom_line(size= 0.7) +
  theme_few() +
  ylab('Average Daily temperature (°C)') + 
  xlab('Day of water year')

624 SD

standard_dev_624 <- daily_wy_aver_624 %>% 
  group_by(waterYear) %>% 
  mutate(residual = (all_ave_temp-aver_ann_temp)+temperature_mean-aver_day_temp) %>% 
  mutate(deviation = abs(residual-lag(residual)))

standard_dev_all_624 <- standard_dev_624 %>% 
  group_by(waterYear) %>% 
  mutate(nmbr = n())

standard_dev_all_624 <- standard_dev_all_624 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

standard_dev_all_624 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1984 8.239005
1985 8.207104
1986 7.191318
1987 7.871848
1989 8.454185
1990 8.200220
1992 6.438238
1995 7.565327
1996 7.835558
1997 7.996219
1998 8.415139
1999 7.174609
2000 7.839800
2001 8.355597
2002 8.492066
2003 8.150297
2004 7.912597
2005 7.915951
2006 7.936724
2007 7.640492
2008 8.021623
2009 7.171130
2010 8.259271
2011 7.932618
2012 7.791063
2013 8.259320
2014 7.372950
2015 6.806548
2016 7.548199
2017 7.108731
2018 7.036935
2019 7.675916
2020 7.741703
ggplot(standard_dev_all_624, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 624 average temperatures for water years 2005-2021

MK & SS for 624 (non-corrected)

sd_mk_624 <- mk.test(standard_dev_all_624$sd_2)
print(sd_mk_624)
## 
##  Mann-Kendall trend test
## 
## data:  standard_dev_all_624$sd_2
## z = -1.9678, n = 33, p-value = 0.04909
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
## -128.0000000 4165.3333333   -0.2424242
sd_sens_624 <- sens.slope(standard_dev_all_624$sd_2)
print(sd_sens_624)
## 
##  Sen's slope
## 
## data:  standard_dev_all_624$sd_2
## z = -1.9678, n = 33, p-value = 0.04909
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.0333661824 -0.0003479011
## sample estimates:
## Sen's slope 
## -0.01662145

Corrected

624 Detrended (corrected)

#using the clean culled df:
#average water year temperature
yearly_wy_aver_624_ad <- snotel_624_adjusted %>% 
  group_by(waterYear) %>% 
  mutate(aver_ann_temp_ad = mean(temp_ad))
#Average temperature by day for all water years:
daily_wy_aver_624_ad <- yearly_wy_aver_624_ad %>% 
  group_by(daymonth) %>% 
  mutate(aver_day_temp_ad = mean(aver_ann_temp_ad))
#average mean temperature by day for the period of record:
daily_wy_aver_624_ad <- daily_wy_aver_624_ad %>% 
  group_by(daymonth) %>% 
  mutate(all_ave_temp_ad = mean(daily_wy_aver_624_ad$aver_day_temp_ad))
# try to show all years as means. 
daily_wy_aver2_624_ad <-daily_wy_aver_624_ad %>% 
  group_by(waterDay) %>%
  mutate(date_temp_ad = mean(temp_ad))
  
daily_wy_aver2_624_ad$date_temp_ad <- signif(daily_wy_aver2_624_ad$date_temp_ad,3) #reduce the sig figs
ggplot(daily_wy_aver2_624_ad, aes(x = waterDay, y = date_temp_ad))+
  geom_line(size= 0.7) +
  theme_few() +
  ylab('Average Daily temperature (°C)') + 
  xlab('Day of water year')

624 SS (corrected)

standard_dev_624_ad <- daily_wy_aver_624_ad %>% 
  group_by(waterYear) %>% 
  #filter(waterYear >= 1987 & waterYear <= 2021) %>% 
  mutate(residual = (all_ave_temp_ad-aver_ann_temp_ad)+temp_ad-aver_day_temp_ad) %>% 
  mutate(deviation = abs(residual-lag(residual)))
standard_dev_all_624_ad <- standard_dev_624_ad %>% 
  group_by(waterYear) %>% 
  mutate(nmbr = n())
standard_dev_all_624_ad <- standard_dev_all_624_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
standard_dev_all_624_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1984 7.738801
1985 7.713059
1986 6.725366
1987 7.374899
1989 7.942209
1990 7.687289
1992 6.080980
1995 7.080976
1996 7.336792
1997 7.519955
1998 7.885653
1999 6.718142
2000 7.329553
2001 7.833709
2002 7.944757
2003 7.617514
2004 7.442397
2005 7.390605
2006 7.304030
2007 7.639661
2008 8.021493
2009 7.170919
2010 8.259153
2011 7.932368
2012 7.790490
2013 8.259010
2014 7.372461
2015 6.806595
2016 7.547948
2017 7.107845
2018 7.036548
2019 7.675791
2020 7.741347
ggplot(standard_dev_all_624_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 624 average temperatures for water years 1986-2021

MK & SS 624 (corrected)

sd_mk_624_ad <- mk.test(standard_dev_all_624_ad$sd_2)
print(sd_mk_624_ad)
## 
##  Mann-Kendall trend test
## 
## data:  standard_dev_all_624_ad$sd_2
## z = 0.35637, n = 33, p-value = 0.7216
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
## 2.400000e+01 4.165333e+03 4.545455e-02
sd_sens_624_ad <- sens.slope(standard_dev_all_624_ad$sd_2)
print(sd_sens_624_ad)
## 
##  Sen's slope
## 
## data:  standard_dev_all_624_ad$sd_2
## z = 0.35637, n = 33, p-value = 0.7216
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.01304163  0.02223675
## sample estimates:
## Sen's slope 
## 0.003307866

Summer and Winter SD NOT-CORRECTED

Summer

summer_standard_dev_all_624 <- standard_dev_624 %>%
  filter(waterDay >= 244 & waterDay <= 335) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())

summer_standard_dev_all_624 <- summer_standard_dev_all_624 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

summer_standard_dev_all_624 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1984 2.371017
1985 1.990557
1986 2.214926
1987 2.127623
1989 3.126720
1990 2.993512
1992 2.774353
1995 3.067086
1996 1.948609
1997 2.086138
1998 2.740390
1999 2.498199
2000 1.663313
2001 2.287905
2002 1.934590
2003 2.763802
2004 2.268487
2005 2.997242
2006 1.968704
2007 2.171153
2008 2.014715
2009 2.529582
2010 2.116445
2011 1.825302
2012 1.620883
2013 1.738808
2014 1.728048
2015 2.122215
2016 2.479053
2017 1.908542
2018 1.727760
2019 2.334135
2020 2.281772
ggplot(summer_standard_dev_all_624, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 624 average summer temperatures for water years 2005-2021

summer MK & SS for 624 (non-corrected)

summer_sd_mk_624 <- mk.test(summer_standard_dev_all_624$sd_2)
print(summer_sd_mk_624)
## 
##  Mann-Kendall trend test
## 
## data:  summer_standard_dev_all_624$sd_2
## z = -2.2157, n = 33, p-value = 0.02671
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
## -144.0000000 4165.3333333   -0.2727273
summer_sd_sens_624 <- sens.slope(summer_standard_dev_all_624$sd_2)
print(summer_sd_sens_624)
## 
##  Sen's slope
## 
## data:  summer_standard_dev_all_624$sd_2
## z = -2.2157, n = 33, p-value = 0.02671
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.032758709 -0.001736526
## sample estimates:
## Sen's slope 
## -0.01764104

Winter

winter_standard_dev_all_624 <- standard_dev_624 %>%
  filter(waterDay >= 32 & waterDay <= 182) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())

winter_standard_dev_all_624 <- winter_standard_dev_all_624 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

winter_standard_dev_all_624 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1984 4.880677
1985 4.967858
1986 4.529985
1987 4.210844
1989 5.844668
1990 5.265678
1992 4.334760
1995 4.484677
1996 4.859324
1997 5.244493
1998 4.464080
1999 4.568526
2000 4.981760
2001 4.150395
2002 5.034307
2003 4.334739
2004 5.397423
2005 4.048105
2006 4.873371
2007 5.229029
2008 5.643945
2009 4.491409
2010 4.672503
2011 5.153151
2012 4.528698
2013 5.749451
2014 4.130044
2015 4.667844
2016 4.917047
2017 4.988580
2018 4.254723
2019 4.126243
2020 4.441848
ggplot(winter_standard_dev_all_624, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 624 average winter temperatures for water years 2005-2021

winter MK & SS for 624 (non-corrected)

winter_sd_mk_624 <- mk.test(winter_standard_dev_all_624$sd_2)
print(winter_sd_mk_624)
## 
##  Mann-Kendall trend test
## 
## data:  winter_standard_dev_all_624$sd_2
## z = -0.72824, n = 33, p-value = 0.4665
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##             S          varS           tau 
##  -48.00000000 4165.33333333   -0.09090909
winter_sd_sens_624 <- sens.slope(winter_standard_dev_all_624$sd_2)
print(winter_sd_sens_624)
## 
##  Sen's slope
## 
## data:  winter_standard_dev_all_624$sd_2
## z = -0.72824, n = 33, p-value = 0.4665
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.02629849  0.01320480
## sample estimates:
##  Sen's slope 
## -0.005315308

Summer and Winter SD CORRECTED

Summer

summer_standard_dev_all_624_ad <- standard_dev_624_ad %>% 
  filter(waterDay >= 244 & waterDay <= 335) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())
summer_standard_dev_all_624_ad <- summer_standard_dev_all_624_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
summer_standard_dev_all_624_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1984 2.145259
1985 1.790887
1986 1.997447
1987 1.914709
1989 2.815434
1990 2.692193
1992 2.529956
1995 2.767172
1996 1.750562
1997 1.882650
1998 2.473916
1999 2.260191
2000 1.490901
2001 2.064709
2002 1.736917
2003 2.482808
2004 2.042300
2005 2.699490
2006 1.867032
2007 2.171811
2008 2.013205
2009 2.528350
2010 2.116246
2011 1.824621
2012 1.619325
2013 1.738330
2014 1.727368
2015 2.123161
2016 2.476465
2017 1.906976
2018 1.725631
2019 2.336097
2020 2.284118
ggplot(summer_standard_dev_all_624_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 624 average summer temperatures for water years 1986-2021

summer MK & SS 624 (corrected)

summer_sd_mk_624_ad <- mk.test(summer_standard_dev_all_624_ad$sd_2)
print(summer_sd_mk_624_ad)
## 
##  Mann-Kendall trend test
## 
## data:  summer_standard_dev_all_624_ad$sd_2
## z = -1.1311, n = 33, p-value = 0.258
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
##  -74.0000000 4165.3333333   -0.1401515
summer_sd_sens_624_ad <- sens.slope(summer_standard_dev_all_624_ad$sd_2)
print(summer_sd_sens_624_ad)
## 
##  Sen's slope
## 
## data:  summer_standard_dev_all_624_ad$sd_2
## z = -1.1311, n = 33, p-value = 0.258
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.02093851  0.00744829
## sample estimates:
## Sen's slope 
## -0.00746448

Winter

winter_standard_dev_all_624_ad <- standard_dev_624_ad %>% 
  filter(waterDay >= 32 & waterDay <= 182) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())
winter_standard_dev_all_624_ad <- winter_standard_dev_all_624_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
winter_standard_dev_all_624_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1984 4.762851
1985 4.874175
1986 4.380676
1987 4.097807
1989 5.676966
1990 5.119591
1992 4.197936
1995 4.351814
1996 4.723854
1997 5.101032
1998 4.342229
1999 4.427507
2000 4.822826
2001 4.059517
2002 4.873722
2003 4.214189
2004 5.243926
2005 3.944212
2006 4.732233
2007 5.226846
2008 5.644781
2009 4.491025
2010 4.672103
2011 5.153036
2012 4.528179
2013 5.749052
2014 4.129798
2015 4.667792
2016 4.916706
2017 4.986515
2018 4.254240
2019 4.126244
2020 4.441082
ggplot(winter_standard_dev_all_624_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 624 average winter temperatures for water years 1986-2021

winter MK & SS 624 (corrected)

winter_sd_mk_624_ad <- mk.test(winter_standard_dev_all_624_ad$sd_2)
print(winter_sd_mk_624_ad)
## 
##  Mann-Kendall trend test
## 
## data:  winter_standard_dev_all_624_ad$sd_2
## z = -0.046483, n = 33, p-value = 0.9629
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##             S          varS           tau 
## -4.000000e+00  4.165333e+03 -7.575758e-03
winter_sd_sens_624_ad <- sens.slope(winter_standard_dev_all_624_ad$sd_2)
print(winter_sd_sens_624_ad)
## 
##  Sen's slope
## 
## data:  winter_standard_dev_all_624_ad$sd_2
## z = -0.046483, n = 33, p-value = 0.9629
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.01984730  0.02049392
## sample estimates:
##   Sen's slope 
## -0.0007865011

Molas Lake 632

NSCE-Morrisey, Bias-Original 10/2/2003

snotel_632 <- SNOTEL_san_juan_Area %>% 
  filter(site_id == "632")
#str(snotel_632) # check the date, usually a character.  

snotel_632$Date <- as.Date(snotel_632$date) #change date from character to date format, capitalize to work with Water year functon from NWIS.

#THIS WILL CHANGE FOR EACH STATION
snotel_632_clean <- snotel_632 %>% # filter for the timeframe
  filter(Date >= "1979-10-01" & Date <= "2022-09-30") %>%
  #filter(temperature_mean >= -30 & temperature_mean <= 20) %>% # removing outliers   
  addWaterYear() %>% 
  mutate(daymonth = format(as.Date(Date), "%d-%m")) %>% 
  na.omit()

#adding water day using difftime (SUPER COOL. example from [this](https://stackoverflow.com/questions/48123049/create-day-index-based-on-water-year))

snotel_632_clean <- snotel_632_clean %>% 
  group_by(waterYear)%>% 
  mutate(waterDay = (as.integer(difftime(Date, ymd(paste0(waterYear - 1 ,'-09-30')), units = "days"))))
# Check for outliers

ggplot(snotel_632_clean, aes(x = Date, y = temperature_mean)) +
  geom_point() + #lwd = 2) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature (°C)') + 
  xlab('Date')

snotel_632_clean <- snotel_632_clean %>% 
  mutate(temp_diff = abs(temperature_min - temperature_max)) %>% 
  #filter(temperature_mean > -40) %>% 
  filter(temperature_mean < 25)
ggplot(snotel_632_clean, aes(x = Date, y = temp_diff)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  #geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature varience (°C)') + 
  xlab('Date')

Per Steven’s advice: :If there are more than 15 missing days, then…remove that year”

# filtering for temperature anomalies
snotel_632_cull_count <- snotel_632_clean %>% 
  filter(temperature_min > -40) %>% 
  count(waterYear)

snotel_632_cull_count
## # A tibble: 37 x 2
## # Groups:   waterYear [37]
##    waterYear     n
##        <dbl> <int>
##  1      1986    54
##  2      1987   355
##  3      1988   362
##  4      1989   364
##  5      1990   365
##  6      1991   365
##  7      1992   366
##  8      1993   303
##  9      1994   294
## 10      1995   361
## # ... with 27 more rows
# filtering for too few observations in a year
snotel_632_cull_count_days <- snotel_632_clean %>% 
  group_by(waterYear) %>% 
  count(waterYear) %>% 
  filter(n < 350)

snotel_632_cull_count_days
## # A tibble: 6 x 2
## # Groups:   waterYear [6]
##   waterYear     n
##       <dbl> <int>
## 1      1986    54
## 2      1993   303
## 3      1994   295
## 4      2005   344
## 5      2006   304
## 6      2013   248

1986, 1993, 1994, 2005, 2006, 2013 need to be culled.

snotel_632_clean_culled <- snotel_632_clean %>% 
  filter(waterYear != "1986" & waterYear != "1993" & waterYear != "1994" & waterYear != "2005" & waterYear != "2006" & waterYear != "2013")# & waterYear != "1993" & waterYear != "1994" & waterYear != "2021" & waterYear != "2022") & waterYear != "2014") #%>% 
  #filter(temperature_mean > -49)

ggplot(snotel_632_clean_culled, aes(x = Date, y = temp_diff)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  #geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature varience (°C)') + 
  xlab('Date')

ggplot(snotel_632_clean_culled, aes(x = Date, y = temperature_mean)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature (°C)') + 
  xlab('Date')

temp_632_xts <- xts(snotel_632_clean_culled$temperature_mean, order.by = snotel_632_clean_culled$Date)

dygraph(temp_632_xts) %>%
  dyAxis("y", label = "Daily mean temperature (°C)") 
snotel_632_clean_culled <- snotel_632_clean_culled %>% 
  filter(temperature_mean < 20)

temp_632_xts <- xts(snotel_632_clean_culled$temperature_mean, order.by = snotel_632_clean_culled$Date)

dygraph(temp_632_xts) %>%
  dyAxis("y", label = "Daily mean temperature (°C)") 

Molas Lake 632

NSCE-Morrisey, Bias-Original 10/2/2003

snotel_632_adjusted <- snotel_632_clean_culled %>%
  mutate(temp_ad = if_else(Date < "2003-10-02", ((5.3*10^(-7))*(temperature_mean^(4))+(3.72*10^(-5))*(temperature_mean^(3))-(2.16*10^(-3))*(temperature_mean^(2))-(7.32*10^(-2))*(temperature_mean)+1.37)+temperature_mean, temperature_mean))

Non-corrected

632 Detrended

#using the clean culled df:

#average water year temperature

yearly_wy_aver_632 <- snotel_632_adjusted %>% 
  group_by(waterYear) %>% 
  mutate(aver_ann_temp = mean(temperature_mean))

#Average temperature by day for all water years:

daily_wy_aver_632 <- yearly_wy_aver_632 %>% 
  group_by(daymonth) %>% 
  mutate(aver_day_temp = mean(aver_ann_temp))

#average mean temperature by day for the period of record:

daily_wy_aver_632 <- daily_wy_aver_632 %>% 
  group_by(daymonth) %>% 
  mutate(all_ave_temp = mean(daily_wy_aver_632$aver_day_temp))

# try to show all years as means. 
daily_wy_aver2_632 <-daily_wy_aver_632 %>% 
  group_by(waterDay) %>%
  mutate(date_temp = mean(temperature_mean))
  

daily_wy_aver2_632$date_temp <- signif(daily_wy_aver2_632$date_temp,3) #reduce the sig figs


ggplot(daily_wy_aver2_632, aes(x = waterDay, y = date_temp))+
  geom_line(size= 0.7) +
  theme_few() +
  ylab('Average Daily temperature (°C)') + 
  xlab('Day of water year')

632 SD

standard_dev_632 <- daily_wy_aver_632 %>% 
  group_by(waterYear) %>% 
  mutate(residual = (all_ave_temp-aver_ann_temp)+temperature_mean-aver_day_temp) %>% 
  mutate(deviation = abs(residual-lag(residual)))

standard_dev_all_632 <- standard_dev_632 %>% 
  group_by(waterYear) %>% 
  mutate(nmbr = n())

standard_dev_all_632 <- standard_dev_all_632 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

standard_dev_all_632 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 7.613234
1988 8.191410
1989 8.166070
1990 7.646926
1991 7.969691
1992 7.156808
1995 7.489888
1996 7.655841
1997 7.698012
1998 8.157837
1999 6.885991
2000 7.657554
2001 6.788385
2002 8.254532
2003 7.907020
2004 7.308264
2007 7.607147
2008 7.964946
2009 6.957483
2010 7.983696
2011 7.701623
2012 7.490054
2014 7.236529
2015 6.581104
2016 7.409875
2017 7.001441
2018 6.814621
2019 7.582515
2020 7.766470
2021 7.792571
2022 7.357711
ggplot(standard_dev_all_632, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 632 average temperatures for water years 2005-2021

MK & SS for 632 (non-corrected)

sd_mk_632 <- mk.test(standard_dev_all_632$sd_2)
print(sd_mk_632)
## 
##  Mann-Kendall trend test
## 
## data:  standard_dev_all_632$sd_2
## z = -1.6317, n = 31, p-value = 0.1028
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
##  -97.0000000 3461.6666667   -0.2086022
sd_sens_632 <- sens.slope(standard_dev_all_632$sd_2)
print(sd_sens_632)
## 
##  Sen's slope
## 
## data:  standard_dev_all_632$sd_2
## z = -1.6317, n = 31, p-value = 0.1028
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.034372524  0.003422909
## sample estimates:
## Sen's slope 
## -0.01583493

Corrected

632 Detrended (corrected)

#using the clean culled df:
#average water year temperature
yearly_wy_aver_632_ad <- snotel_632_adjusted %>% 
  group_by(waterYear) %>% 
  mutate(aver_ann_temp_ad = mean(temp_ad))
#Average temperature by day for all water years:
daily_wy_aver_632_ad <- yearly_wy_aver_632_ad %>% 
  group_by(daymonth) %>% 
  mutate(aver_day_temp_ad = mean(aver_ann_temp_ad))
#average mean temperature by day for the period of record:
daily_wy_aver_632_ad <- daily_wy_aver_632_ad %>% 
  group_by(daymonth) %>% 
  mutate(all_ave_temp_ad = mean(daily_wy_aver_632_ad$aver_day_temp_ad))
# try to show all years as means. 
daily_wy_aver2_632_ad <-daily_wy_aver_632_ad %>% 
  group_by(waterDay) %>%
  mutate(date_temp_ad = mean(temp_ad))
  
daily_wy_aver2_632_ad$date_temp_ad <- signif(daily_wy_aver2_632_ad$date_temp_ad,3) #reduce the sig figs
ggplot(daily_wy_aver2_632_ad, aes(x = waterDay, y = date_temp_ad))+
  geom_line(size= 0.7) +
  theme_few() +
  ylab('Average Daily temperature (°C)') + 
  xlab('Day of water year')

632 SS (corrected)

standard_dev_632_ad <- daily_wy_aver_632_ad %>% 
  group_by(waterYear) %>% 
  #filter(waterYear >= 1987 & waterYear <= 2021) %>% 
  mutate(residual = (all_ave_temp_ad-aver_ann_temp_ad)+temp_ad-aver_day_temp_ad) %>% 
  mutate(deviation = abs(residual-lag(residual)))
standard_dev_all_632_ad <- standard_dev_632_ad %>% 
  group_by(waterYear) %>% 
  mutate(nmbr = n())
standard_dev_all_632_ad <- standard_dev_all_632_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
standard_dev_all_632_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 7.106416
1988 7.678134
1989 7.662133
1990 7.141729
1991 7.492618
1992 6.671512
1995 6.957618
1996 7.110780
1997 7.178235
1998 7.586041
1999 6.397699
2000 7.103150
2001 6.358660
2002 7.672831
2003 7.338249
2004 7.309361
2007 7.606845
2008 7.965266
2009 6.957636
2010 7.984148
2011 7.701741
2012 7.490245
2014 7.236308
2015 6.581364
2016 7.409996
2017 7.000722
2018 6.815246
2019 7.583475
2020 7.766635
2021 7.793272
2022 7.358050
ggplot(standard_dev_all_632_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 632 average temperatures for water years 1986-2021

MK & SS 632 (corrected)

sd_mk_632_ad <- mk.test(standard_dev_all_632_ad$sd_2)
print(sd_mk_632_ad)
## 
##  Mann-Kendall trend test
## 
## data:  standard_dev_all_632_ad$sd_2
## z = 0.9518, n = 31, p-value = 0.3412
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
##   57.0000000 3461.6666667    0.1225806
sd_sens_632_ad <- sens.slope(standard_dev_all_632_ad$sd_2)
print(sd_sens_632_ad)
## 
##  Sen's slope
## 
## data:  standard_dev_all_632_ad$sd_2
## z = 0.9518, n = 31, p-value = 0.3412
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.01086011  0.02505935
## sample estimates:
## Sen's slope 
## 0.008387789

Summer and Winter SD NOT-CORRECTED

Summer

summer_standard_dev_all_632 <- standard_dev_632 %>%
  filter(waterDay >= 244 & waterDay <= 335) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())

summer_standard_dev_all_632 <- summer_standard_dev_all_632 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

summer_standard_dev_all_632 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 2.114538
1988 2.228749
1989 2.856810
1990 2.412193
1991 2.352444
1992 2.653405
1995 3.167125
1996 2.098569
1997 2.061274
1998 2.877816
1999 2.505380
2000 1.633471
2001 2.886228
2002 2.116931
2003 2.603805
2004 2.098651
2007 2.150642
2008 2.108596
2009 2.560834
2010 2.129920
2011 1.670740
2012 1.544598
2014 1.887934
2015 2.090604
2016 2.291709
2017 1.837367
2018 1.515691
2019 2.268798
2020 2.134924
2021 1.997591
2022 1.865141
ggplot(summer_standard_dev_all_632, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 632 average summer temperatures for water years 2005-2021

summer MK & SS for 632 (non-corrected)

summer_sd_mk_632 <- mk.test(summer_standard_dev_all_632$sd_2)
print(summer_sd_mk_632)
## 
##  Mann-Kendall trend test
## 
## data:  summer_standard_dev_all_632$sd_2
## z = -2.6514, n = 31, p-value = 0.008015
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
## -157.0000000 3461.6666667   -0.3376344
summer_sd_sens_632 <- sens.slope(summer_standard_dev_all_632$sd_2)
print(summer_sd_sens_632)
## 
##  Sen's slope
## 
## data:  summer_standard_dev_all_632$sd_2
## z = -2.6514, n = 31, p-value = 0.008015
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.039099584 -0.005737351
## sample estimates:
## Sen's slope 
## -0.02039296

Winter

winter_standard_dev_all_632 <- standard_dev_632 %>%
  filter(waterDay >= 32 & waterDay <= 182) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())

winter_standard_dev_all_632 <- winter_standard_dev_all_632 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

winter_standard_dev_all_632 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 3.949897
1988 4.829572
1989 5.622923
1990 4.743667
1991 4.793091
1992 3.588398
1995 4.242979
1996 4.655088
1997 4.822418
1998 4.285365
1999 4.113247
2000 4.708228
2001 3.950374
2002 4.915860
2003 4.107473
2004 4.960115
2007 5.041141
2008 5.329581
2009 4.270156
2010 4.210609
2011 5.136480
2012 4.073954
2014 3.979979
2015 4.388198
2016 4.767132
2017 5.064705
2018 4.097621
2019 3.928620
2020 4.386364
2021 4.169782
2022 4.469723
ggplot(winter_standard_dev_all_632, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 632 average winter temperatures for water years 2005-2021

winter MK & SS for 632 (non-corrected)

winter_sd_mk_632 <- mk.test(winter_standard_dev_all_632$sd_2)
print(winter_sd_mk_632)
## 
##  Mann-Kendall trend test
## 
## data:  winter_standard_dev_all_632$sd_2
## z = -0.50989, n = 31, p-value = 0.6101
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##             S          varS           tau 
##  -31.00000000 3461.66666667   -0.06666667
winter_sd_sens_632 <- sens.slope(winter_standard_dev_all_632$sd_2)
print(winter_sd_sens_632)
## 
##  Sen's slope
## 
## data:  winter_standard_dev_all_632$sd_2
## z = -0.50989, n = 31, p-value = 0.6101
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.02934037  0.01517316
## sample estimates:
##  Sen's slope 
## -0.004429864

Summer and Winter SD CORRECTED

Summer

summer_standard_dev_all_632_ad <- standard_dev_632_ad %>% 
  filter(waterDay >= 244 & waterDay <= 335) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())
summer_standard_dev_all_632_ad <- summer_standard_dev_all_632_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
summer_standard_dev_all_632_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 1.906484
1988 2.015215
1989 2.570479
1990 2.171287
1991 2.126432
1992 2.395029
1995 2.849838
1996 1.882298
1997 1.856281
1998 2.592413
1999 2.262767
2000 1.464044
2001 2.625171
2002 1.897873
2003 2.334779
2004 2.097548
2007 2.150936
2008 2.109568
2009 2.560281
2010 2.129304
2011 1.670271
2012 1.543369
2014 1.887474
2015 2.090877
2016 2.290935
2017 1.836412
2018 1.515104
2019 2.269449
2020 2.135390
2021 1.997565
2022 1.865564
ggplot(summer_standard_dev_all_632_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 632 average summer temperatures for water years 1986-2021

summer MK & SS 632 (corrected)

summer_sd_mk_632_ad <- mk.test(summer_standard_dev_all_632_ad$sd_2)
print(summer_sd_mk_632_ad)
## 
##  Mann-Kendall trend test
## 
## data:  summer_standard_dev_all_632_ad$sd_2
## z = -1.5637, n = 31, p-value = 0.1179
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##        S     varS      tau 
##  -93.000 3461.667   -0.200
summer_sd_sens_632_ad <- sens.slope(summer_standard_dev_all_632_ad$sd_2)
print(summer_sd_sens_632_ad)
## 
##  Sen's slope
## 
## data:  summer_standard_dev_all_632_ad$sd_2
## z = -1.5637, n = 31, p-value = 0.1179
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.02586115  0.00291086
## sample estimates:
##  Sen's slope 
## -0.009778507

Winter

winter_standard_dev_all_632_ad <- standard_dev_632_ad %>% 
  filter(waterDay >= 32 & waterDay <= 182) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())
winter_standard_dev_all_632_ad <- winter_standard_dev_all_632_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
winter_standard_dev_all_632_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 3.822176
1988 4.707213
1989 5.460606
1990 4.581902
1991 4.707497
1992 3.453982
1995 4.078706
1996 4.474750
1997 4.640139
1998 4.120702
1999 3.953304
2000 4.510282
2001 3.819726
2002 4.720494
2003 3.965848
2004 4.959795
2007 5.041005
2008 5.330231
2009 4.270758
2010 4.211804
2011 5.136593
2012 4.075229
2014 3.980001
2015 4.388367
2016 4.767229
2017 5.064022
2018 4.099260
2019 3.931958
2020 4.386891
2021 4.171687
2022 4.470494
ggplot(winter_standard_dev_all_632_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 632 average winter temperatures for water years 1986-2021

winter MK & SS 632 (corrected)

winter_sd_mk_632_ad <- mk.test(winter_standard_dev_all_632_ad$sd_2)
print(winter_sd_mk_632_ad)
## 
##  Mann-Kendall trend test
## 
## data:  winter_standard_dev_all_632_ad$sd_2
## z = 0.067986, n = 31, p-value = 0.9458
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
## 5.000000e+00 3.461667e+03 1.075269e-02
winter_sd_sens_632_ad <- sens.slope(winter_standard_dev_all_632_ad$sd_2)
print(winter_sd_sens_632_ad)
## 
##  Sen's slope
## 
## data:  winter_standard_dev_all_632_ad$sd_2
## z = 0.067986, n = 31, p-value = 0.9458
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.02224021  0.02070360
## sample estimates:
## Sen's slope 
## 0.001106788

Red Mountain Pass 713

Morrisey 8/18/2004

snotel_713 <- SNOTEL_san_juan_Area %>% 
  filter(site_id == "713")
#str(snotel_713) # check the date, usually a character.  

snotel_713$Date <- as.Date(snotel_713$date) #change date from character to date format, capitalize to work with Water year functon from NWIS.

#THIS WILL CHANGE FOR EACH STATION
snotel_713_clean <- snotel_713 %>% # filter for the timeframe
  filter(Date >= "1979-10-01" & Date <= "2022-09-30") %>%
  #filter(temperature_mean >= -30 & temperature_mean <= 20) %>% # removing outliers   
  addWaterYear() %>% 
  mutate(daymonth = format(as.Date(Date), "%d-%m")) %>% 
  na.omit()

#adding water day using difftime (SUPER COOL. example from [this](https://stackoverflow.com/questions/48123049/create-day-index-based-on-water-year))

snotel_713_clean <- snotel_713_clean %>% 
  group_by(waterYear)%>% 
  mutate(waterDay = (as.integer(difftime(Date, ymd(paste0(waterYear - 1 ,'-09-30')), units = "days"))))
# Check for outliers

ggplot(snotel_713_clean, aes(x = Date, y = temperature_mean)) +
  geom_point() + #lwd = 2) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature (°C)') + 
  xlab('Date')

snotel_713_clean <- snotel_713_clean %>% 
  mutate(temp_diff = abs(temperature_min - temperature_max)) %>% 
  filter(temperature_mean > -40) %>% 
  filter(temperature_mean < 25)
ggplot(snotel_713_clean, aes(x = Date, y = temp_diff)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  #geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature varience (°C)') + 
  xlab('Date')

Per Steven’s advice: :If there are more than 15 missing days, then…remove that year”

# filtering for temperature anomalies
snotel_713_cull_count <- snotel_713_clean %>% 
  filter(temperature_min > -40) %>% 
  count(waterYear)

snotel_713_cull_count
## # A tibble: 42 x 2
## # Groups:   waterYear [42]
##    waterYear     n
##        <dbl> <int>
##  1      1981   189
##  2      1982   287
##  3      1983   319
##  4      1984   360
##  5      1985   362
##  6      1986   364
##  7      1987   263
##  8      1988   282
##  9      1989   228
## 10      1990   365
## # ... with 32 more rows
# filtering for too few observations in a year
snotel_713_cull_count_days <- snotel_713_clean %>% 
  group_by(waterYear) %>% 
  count(waterYear) %>% 
  filter(n < 350)

snotel_713_cull_count_days
## # A tibble: 6 x 2
## # Groups:   waterYear [6]
##   waterYear     n
##       <dbl> <int>
## 1      1981   189
## 2      1982   287
## 3      1983   319
## 4      1987   315
## 5      1989   263
## 6      1994   308

1981, 1982, 1983, 1987, 1989, 1994 need to be culled. 1984 & 1985 are too low.

snotel_713_clean_culled <- snotel_713_clean %>% 
  filter(waterYear > "1985" & waterYear != "1987" & waterYear != "1989" & waterYear != "1994")# & waterYear != "1993" & waterYear != "1994" & waterYear != "2021" & waterYear != "2022") & waterYear != "2014") #%>% 
  #filter(temperature_mean > -49)

ggplot(snotel_713_clean_culled, aes(x = Date, y = temp_diff)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  #geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature varience (°C)') + 
  xlab('Date')

ggplot(snotel_713_clean_culled, aes(x = Date, y = temperature_mean)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature (°C)') + 
  xlab('Date')

temp_713_xts <- xts(snotel_713_clean_culled$temperature_mean, order.by = snotel_713_clean_culled$Date)

dygraph(temp_713_xts) %>%
  dyAxis("y", label = "Daily mean temperature (°C)") 
snotel_713_clean_culled <- snotel_713_clean_culled %>% 
  filter(temperature_mean < 19.3)

temp_713_xts <- xts(snotel_713_clean_culled$temperature_mean, order.by = snotel_713_clean_culled$Date)

dygraph(temp_713_xts) %>%
  dyAxis("y", label = "Daily mean temperature (°C)") 

Red Mountain Pass 713

Morrisey 8/18/2004

snotel_713_adjusted <- snotel_713_clean_culled %>%
  mutate(temp_ad = if_else(Date < "2004-08-18", ((5.3*10^(-7))*(temperature_mean^(4))+(3.72*10^(-5))*(temperature_mean^(3))-(2.16*10^(-3))*(temperature_mean^(2))-(7.32*10^(-2))*(temperature_mean)+1.37)+temperature_mean, temperature_mean))

Non-corrected

713 Detrended

#using the clean culled df:

#average water year temperature

yearly_wy_aver_713 <- snotel_713_adjusted %>% 
  group_by(waterYear) %>% 
  mutate(aver_ann_temp = mean(temperature_mean))

#Average temperature by day for all water years:

daily_wy_aver_713 <- yearly_wy_aver_713 %>% 
  group_by(daymonth) %>% 
  mutate(aver_day_temp = mean(aver_ann_temp))

#average mean temperature by day for the period of record:

daily_wy_aver_713 <- daily_wy_aver_713 %>% 
  group_by(daymonth) %>% 
  mutate(all_ave_temp = mean(daily_wy_aver_713$aver_day_temp))

# try to show all years as means. 
daily_wy_aver2_713 <-daily_wy_aver_713 %>% 
  group_by(waterDay) %>%
  mutate(date_temp = mean(temperature_mean))
  

daily_wy_aver2_713$date_temp <- signif(daily_wy_aver2_713$date_temp,3) #reduce the sig figs


ggplot(daily_wy_aver2_713, aes(x = waterDay, y = date_temp))+
  geom_line(size= 0.7) +
  theme_few() +
  ylab('Average Daily temperature (°C)') + 
  xlab('Day of water year')

713 SD

standard_dev_713 <- daily_wy_aver_713 %>% 
  group_by(waterYear) %>% 
  mutate(residual = (all_ave_temp-aver_ann_temp)+temperature_mean-aver_day_temp) %>% 
  mutate(deviation = abs(residual-lag(residual)))

standard_dev_all_713 <- standard_dev_713 %>% 
  group_by(waterYear) %>% 
  mutate(nmbr = n())

standard_dev_all_713 <- standard_dev_all_713 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

standard_dev_all_713 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1986 7.022683
1988 9.344095
1990 7.761743
1991 7.768555
1992 7.212083
1993 7.716280
1995 7.336781
1996 7.731429
1997 7.947757
1998 8.198019
1999 6.921528
2000 7.680921
2001 8.089853
2002 8.395619
2003 7.952564
2004 7.777488
2005 7.151965
2006 7.312223
2007 7.636775
2008 7.908986
2009 6.940732
2010 7.877650
2011 7.730288
2012 7.531887
2013 8.195738
2014 7.284701
2015 6.638343
2016 7.380700
2017 7.097935
2018 7.002862
2019 7.626598
2020 7.810018
2021 7.817233
2022 7.339213
ggplot(standard_dev_all_713, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 713 average temperatures for water years 2005-2021

MK & SS for 713 (non-corrected)

sd_mk_713 <- mk.test(standard_dev_all_713$sd_2)
print(sd_mk_713)
## 
##  Mann-Kendall trend test
## 
## data:  standard_dev_all_713$sd_2
## z = -1.1563, n = 34, p-value = 0.2476
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##          S       varS        tau 
##  -79.00000 4550.33333   -0.14082
sd_sens_713 <- sens.slope(standard_dev_all_713$sd_2)
print(sd_sens_713)
## 
##  Sen's slope
## 
## data:  standard_dev_all_713$sd_2
## z = -1.1563, n = 34, p-value = 0.2476
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.028289222  0.006454843
## sample estimates:
##  Sen's slope 
## -0.008980292

Corrected

713 Detrended (corrected)

#using the clean culled df:
#average water year temperature
yearly_wy_aver_713_ad <- snotel_713_adjusted %>% 
  group_by(waterYear) %>% 
  mutate(aver_ann_temp_ad = mean(temp_ad))
#Average temperature by day for all water years:
daily_wy_aver_713_ad <- yearly_wy_aver_713_ad %>% 
  group_by(daymonth) %>% 
  mutate(aver_day_temp_ad = mean(aver_ann_temp_ad))
#average mean temperature by day for the period of record:
daily_wy_aver_713_ad <- daily_wy_aver_713_ad %>% 
  group_by(daymonth) %>% 
  mutate(all_ave_temp_ad = mean(daily_wy_aver_713_ad$aver_day_temp_ad))
# try to show all years as means. 
daily_wy_aver2_713_ad <-daily_wy_aver_713_ad %>% 
  group_by(waterDay) %>%
  mutate(date_temp_ad = mean(temp_ad))
  
daily_wy_aver2_713_ad$date_temp_ad <- signif(daily_wy_aver2_713_ad$date_temp_ad,3) #reduce the sig figs
ggplot(daily_wy_aver2_713_ad, aes(x = waterDay, y = date_temp_ad))+
  geom_line(size= 0.7) +
  theme_few() +
  ylab('Average Daily temperature (°C)') + 
  xlab('Day of water year')

713 SS (corrected)

standard_dev_713_ad <- daily_wy_aver_713_ad %>% 
  group_by(waterYear) %>% 
  #filter(waterYear >= 1987 & waterYear <= 2021) %>% 
  mutate(residual = (all_ave_temp_ad-aver_ann_temp_ad)+temp_ad-aver_day_temp_ad) %>% 
  mutate(deviation = abs(residual-lag(residual)))
standard_dev_all_713_ad <- standard_dev_713_ad %>% 
  group_by(waterYear) %>% 
  mutate(nmbr = n())
standard_dev_all_713_ad <- standard_dev_all_713_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
standard_dev_all_713_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1986 6.564707
1988 8.872885
1990 7.267850
1991 7.311859
1992 6.739882
1993 7.221869
1995 6.846024
1996 7.223659
1997 7.456352
1998 7.661532
1999 6.466454
2000 7.159552
2001 7.557097
2002 7.840372
2003 7.411227
2004 7.238021
2005 7.151835
2006 7.312105
2007 7.636625
2008 7.909548
2009 6.940894
2010 7.878182
2011 7.730889
2012 7.532385
2013 8.196125
2014 7.284711
2015 6.639012
2016 7.380681
2017 7.097919
2018 7.003100
2019 7.627224
2020 7.810395
2021 7.817455
2022 7.339782
ggplot(standard_dev_all_713_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 713 average temperatures for water years 1986-2021

MK & SS 713 (corrected)

sd_mk_713_ad <- mk.test(standard_dev_all_713_ad$sd_2)
print(sd_mk_713_ad)
## 
##  Mann-Kendall trend test
## 
## data:  standard_dev_all_713_ad$sd_2
## z = 1.4231, n = 34, p-value = 0.1547
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
##   97.0000000 4550.3333333    0.1729055
sd_sens_713_ad <- sens.slope(standard_dev_all_713_ad$sd_2)
print(sd_sens_713_ad)
## 
##  Sen's slope
## 
## data:  standard_dev_all_713_ad$sd_2
## z = 1.4231, n = 34, p-value = 0.1547
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.005520634  0.030221234
## sample estimates:
## Sen's slope 
##  0.01194811

Summer and Winter SD NOT-CORRECTED

Summer

summer_standard_dev_all_713 <- standard_dev_713 %>%
  filter(waterDay >= 244 & waterDay <= 335) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())

summer_standard_dev_all_713 <- summer_standard_dev_all_713 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

summer_standard_dev_all_713 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1986 2.139350
1988 2.406641
1990 2.336632
1991 2.159360
1992 2.668072
1993 2.909805
1995 3.136742
1996 2.080807
1997 2.074595
1998 2.957249
1999 2.563673
2000 1.773197
2001 2.337354
2002 2.294479
2003 2.670927
2004 2.236947
2005 2.812376
2006 1.847511
2007 2.321335
2008 2.175636
2009 2.588092
2010 2.233155
2011 1.878950
2012 1.571661
2013 1.836963
2014 1.930513
2015 2.189941
2016 2.408465
2017 1.768913
2018 1.593608
2019 2.343849
2020 2.338442
2021 2.070922
2022 1.836083
ggplot(summer_standard_dev_all_713, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 713 average summer temperatures for water years 2005-2021

summer MK & SS for 713 (non-corrected)

summer_sd_mk_713 <- mk.test(summer_standard_dev_all_713$sd_2)
print(summer_sd_mk_713)
## 
##  Mann-Kendall trend test
## 
## data:  summer_standard_dev_all_713$sd_2
## z = -2.3423, n = 34, p-value = 0.01917
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
## -159.0000000 4550.3333333   -0.2834225
summer_sd_sens_713 <- sens.slope(summer_standard_dev_all_713$sd_2)
print(summer_sd_sens_713)
## 
##  Sen's slope
## 
## data:  summer_standard_dev_all_713$sd_2
## z = -2.3423, n = 34, p-value = 0.01917
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.03129544 -0.00304958
## sample estimates:
## Sen's slope 
## -0.01695302

Winter

winter_standard_dev_all_713 <- standard_dev_713 %>%
  filter(waterDay >= 32 & waterDay <= 182) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())

winter_standard_dev_all_713 <- winter_standard_dev_all_713 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

winter_standard_dev_all_713 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1986 4.645769
1988 4.862335
1990 4.799414
1991 4.611532
1992 3.437104
1993 3.834979
1995 4.170829
1996 4.759220
1997 5.077174
1998 4.272264
1999 4.270013
2000 4.764869
2001 3.924418
2002 4.865435
2003 4.117591
2004 5.142411
2005 3.839183
2006 4.571793
2007 5.128933
2008 5.379638
2009 4.271019
2010 4.221595
2011 5.159513
2012 4.142368
2013 5.487970
2014 4.088167
2015 4.383886
2016 4.562763
2017 5.084893
2018 4.324668
2019 3.901566
2020 4.282591
2021 4.146951
2022 4.474188
ggplot(winter_standard_dev_all_713, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 713 average winter temperatures for water years 2005-2021

winter MK & SS for 713 (non-corrected)

winter_sd_mk_713 <- mk.test(winter_standard_dev_all_713$sd_2)
print(winter_sd_mk_713)
## 
##  Mann-Kendall trend test
## 
## data:  winter_standard_dev_all_713$sd_2
## z = -0.059298, n = 34, p-value = 0.9527
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##             S          varS           tau 
## -5.000000e+00  4.550333e+03 -8.912656e-03
winter_sd_sens_713 <- sens.slope(winter_standard_dev_all_713$sd_2)
print(winter_sd_sens_713)
## 
##  Sen's slope
## 
## data:  winter_standard_dev_all_713$sd_2
## z = -0.059298, n = 34, p-value = 0.9527
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.01975444  0.01876824
## sample estimates:
##   Sen's slope 
## -0.0009183803

Summer and Winter SD CORRECTED

Summer

summer_standard_dev_all_713_ad <- standard_dev_713_ad %>% 
  filter(waterDay >= 244 & waterDay <= 335) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())
summer_standard_dev_all_713_ad <- summer_standard_dev_all_713_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
summer_standard_dev_all_713_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1986 1.930084
1988 2.180706
1990 2.105669
1991 1.954594
1992 2.412392
1993 2.630912
1995 2.830362
1996 1.872357
1997 1.873575
1998 2.671939
1999 2.322147
2000 1.592045
2001 2.114503
2002 2.061693
2003 2.398854
2004 2.115890
2005 2.812773
2006 1.848523
2007 2.321810
2008 2.174991
2009 2.588728
2010 2.233945
2011 1.880062
2012 1.571792
2013 1.837758
2014 1.932501
2015 2.191267
2016 2.407967
2017 1.767961
2018 1.592847
2019 2.344936
2020 2.338940
2021 2.070055
2022 1.835797
ggplot(summer_standard_dev_all_713_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 713 average summer temperatures for water years 1986-2021

summer MK & SS 713 (corrected)

summer_sd_mk_713_ad <- mk.test(summer_standard_dev_all_713_ad$sd_2)
print(summer_sd_mk_713_ad)
## 
##  Mann-Kendall trend test
## 
## data:  summer_standard_dev_all_713_ad$sd_2
## z = -1.097, n = 34, p-value = 0.2726
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
##  -75.0000000 4550.3333333   -0.1336898
summer_sd_sens_713_ad <- sens.slope(summer_standard_dev_all_713_ad$sd_2)
print(summer_sd_sens_713_ad)
## 
##  Sen's slope
## 
## data:  summer_standard_dev_all_713_ad$sd_2
## z = -1.097, n = 34, p-value = 0.2726
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.019882565  0.006751377
## sample estimates:
##  Sen's slope 
## -0.005790124

Winter

winter_standard_dev_all_713_ad <- standard_dev_713_ad %>% 
  filter(waterDay >= 32 & waterDay <= 182) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())
winter_standard_dev_all_713_ad <- winter_standard_dev_all_713_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
winter_standard_dev_all_713_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1986 4.494194
1988 4.789179
1990 4.649945
1991 4.527027
1992 3.325878
1993 3.732987
1995 4.029498
1996 4.612093
1997 4.916879
1998 4.137738
1999 4.130153
2000 4.590334
2001 3.816445
2002 4.705872
2003 3.993888
2004 4.979402
2005 3.839378
2006 4.572419
2007 5.128989
2008 5.380547
2009 4.271172
2010 4.223062
2011 5.160807
2012 4.143686
2013 5.489179
2014 4.088688
2015 4.384924
2016 4.562407
2017 5.085927
2018 4.325059
2019 3.903572
2020 4.283199
2021 4.148313
2022 4.476398
ggplot(winter_standard_dev_all_713_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 713 average winter temperatures for water years 1986-2021

winter MK & SS 713 (corrected)

winter_sd_mk_713_ad <- mk.test(winter_standard_dev_all_713_ad$sd_2)
print(winter_sd_mk_713_ad)
## 
##  Mann-Kendall trend test
## 
## data:  winter_standard_dev_all_713_ad$sd_2
## z = 0.44473, n = 34, p-value = 0.6565
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
## 3.100000e+01 4.550333e+03 5.525847e-02
winter_sd_sens_713_ad <- sens.slope(winter_standard_dev_all_713_ad$sd_2)
print(winter_sd_sens_713_ad)
## 
##  Sen's slope
## 
## data:  winter_standard_dev_all_713_ad$sd_2
## z = 0.44473, n = 34, p-value = 0.6565
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.01433478  0.02256352
## sample estimates:
## Sen's slope 
## 0.005733297

Scotch Creek 739

Morrisey 6/15/2004

snotel_739 <- SNOTEL_san_juan_Area %>% 
  filter(site_id == "739")
#str(snotel_739) # check the date, usually a character.  

snotel_739$Date <- as.Date(snotel_739$date) #change date from character to date format, capitalize to work with Water year functon from NWIS.

#THIS WILL CHANGE FOR EACH STATION
snotel_739_clean <- snotel_739 %>% # filter for the timeframe
  filter(Date >= "1979-10-01" & Date <= "2022-09-30") %>%
  #filter(temperature_mean >= -30 & temperature_mean <= 20) %>% # removing outliers   
  addWaterYear() %>% 
  mutate(daymonth = format(as.Date(Date), "%d-%m")) %>% 
  na.omit()

#adding water day using difftime (SUPER COOL. example from [this](https://stackoverflow.com/questions/48123049/create-day-index-based-on-water-year))

snotel_739_clean <- snotel_739_clean %>% 
  group_by(waterYear)%>% 
  mutate(waterDay = (as.integer(difftime(Date, ymd(paste0(waterYear - 1 ,'-09-30')), units = "days"))))
# Check for outliers

ggplot(snotel_739_clean, aes(x = Date, y = temperature_mean)) +
  geom_point() + #lwd = 2) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature (°C)') + 
  xlab('Date')

snotel_739_clean <- snotel_739_clean %>% 
  mutate(temp_diff = abs(temperature_min - temperature_max)) %>% 
  filter(temperature_mean > -40) %>% 
  filter(temperature_mean < 25)
ggplot(snotel_739_clean, aes(x = Date, y = temp_diff)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  #geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature varience (°C)') + 
  xlab('Date')

Per Steven’s advice: :If there are more than 15 missing days, then…remove that year”

# filtering for temperature anomalies
snotel_739_cull_count <- snotel_739_clean %>% 
  filter(temperature_min > -40) %>% 
  count(waterYear)

snotel_739_cull_count
## # A tibble: 36 x 2
## # Groups:   waterYear [36]
##    waterYear     n
##        <dbl> <int>
##  1      1987   345
##  2      1988   273
##  3      1989   363
##  4      1990   365
##  5      1991   365
##  6      1992   365
##  7      1993   342
##  8      1994   345
##  9      1995   362
## 10      1996   365
## # ... with 26 more rows
# filtering for too few observations in a year
snotel_739_cull_count_days <- snotel_739_clean %>% 
  group_by(waterYear) %>% 
  count(waterYear) %>% 
  filter(n < 350)

snotel_739_cull_count_days
## # A tibble: 6 x 2
## # Groups:   waterYear [6]
##   waterYear     n
##       <dbl> <int>
## 1      1987   345
## 2      1988   273
## 3      1993   343
## 4      1994   345
## 5      2016   320
## 6      2017   249

1987, 1988, 1993, 1994, 2016, 2017 need to be culled.

snotel_739_clean_culled <- snotel_739_clean %>% 
  filter(waterYear != "1987" & waterYear != "1988" & waterYear != "1993" & waterYear != "1994" & waterYear != "2016" & waterYear != "2017")# & waterYear != "1993" & waterYear != "1994" & waterYear != "2021" & waterYear != "2022") & waterYear != "2014") #%>% 
  #filter(temperature_mean > -49)

ggplot(snotel_739_clean_culled, aes(x = Date, y = temp_diff)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  #geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature varience (°C)') + 
  xlab('Date')

ggplot(snotel_739_clean_culled, aes(x = Date, y = temperature_mean)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature (°C)') + 
  xlab('Date')

temp_739_xts <- xts(snotel_739_clean_culled$temperature_mean, order.by = snotel_739_clean_culled$Date)

dygraph(temp_739_xts) %>%
  dyAxis("y", label = "Daily mean temperature (°C)") 
snotel_739_clean_culled <- snotel_739_clean_culled %>% 
  filter(temperature_mean < 19.3)

temp_739_xts <- xts(snotel_739_clean_culled$temperature_mean, order.by = snotel_739_clean_culled$Date)

dygraph(temp_739_xts) %>%
  dyAxis("y", label = "Daily mean temperature (°C)") 

Scotch Creek 739

Morrisey 6/15/2004

snotel_739_adjusted <- snotel_739_clean_culled %>%
  mutate(temp_ad = if_else(Date < "2004-06-15", ((5.3*10^(-7))*(temperature_mean^(4))+(3.72*10^(-5))*(temperature_mean^(3))-(2.16*10^(-3))*(temperature_mean^(2))-(7.32*10^(-2))*(temperature_mean)+1.37)+temperature_mean, temperature_mean))

Non-corrected

739 Detrended

#using the clean culled df:

#average water year temperature

yearly_wy_aver_739 <- snotel_739_adjusted %>% 
  group_by(waterYear) %>% 
  mutate(aver_ann_temp = mean(temperature_mean))

#Average temperature by day for all water years:

daily_wy_aver_739 <- yearly_wy_aver_739 %>% 
  group_by(daymonth) %>% 
  mutate(aver_day_temp = mean(aver_ann_temp))

#average mean temperature by day for the period of record:

daily_wy_aver_739 <- daily_wy_aver_739 %>% 
  group_by(daymonth) %>% 
  mutate(all_ave_temp = mean(daily_wy_aver_739$aver_day_temp))

# try to show all years as means. 
daily_wy_aver2_739 <-daily_wy_aver_739 %>% 
  group_by(waterDay) %>%
  mutate(date_temp = mean(temperature_mean))
  

daily_wy_aver2_739$date_temp <- signif(daily_wy_aver2_739$date_temp,3) #reduce the sig figs


ggplot(daily_wy_aver2_739, aes(x = waterDay, y = date_temp))+
  geom_line(size= 0.7) +
  theme_few() +
  ylab('Average Daily temperature (°C)') + 
  xlab('Day of water year')

739 SD

standard_dev_739 <- daily_wy_aver_739 %>% 
  group_by(waterYear) %>% 
  mutate(residual = (all_ave_temp-aver_ann_temp)+temperature_mean-aver_day_temp) %>% 
  mutate(deviation = abs(residual-lag(residual)))

standard_dev_all_739 <- standard_dev_739 %>% 
  group_by(waterYear) %>% 
  mutate(nmbr = n())

standard_dev_all_739 <- standard_dev_all_739 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

standard_dev_all_739 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1989 7.327097
1990 6.021389
1991 5.989983
1992 4.863013
1995 7.600315
1996 7.840086
1997 7.952598
1998 8.393336
1999 7.124567
2000 7.697346
2001 8.108824
2002 8.727256
2003 7.977395
2004 7.894210
2005 7.144352
2006 7.250093
2007 7.615681
2008 7.916198
2009 6.788880
2010 7.880288
2011 7.703849
2012 7.458561
2013 8.171355
2014 7.223604
2015 6.545241
2018 6.908918
2019 7.558347
2020 7.673725
2021 7.658883
2022 7.427996
ggplot(standard_dev_all_739, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 739 average temperatures for water years 2005-2021

MK & SS for 739 (non-corrected)

sd_mk_739 <- mk.test(standard_dev_all_739$sd_2)
print(sd_mk_739)
## 
##  Mann-Kendall trend test
## 
## data:  standard_dev_all_739$sd_2
## z = 0, n = 30, p-value = 1
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
## 1.000000e+00 3.141667e+03 2.298851e-03
sd_sens_739 <- sens.slope(standard_dev_all_739$sd_2)
print(sd_sens_739)
## 
##  Sen's slope
## 
## data:  standard_dev_all_739$sd_2
## z = 0, n = 30, p-value = 1
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.02559406  0.03474477
## sample estimates:
##  Sen's slope 
## 0.0005911373

Corrected

739 Detrended (corrected)

#using the clean culled df:
#average water year temperature
yearly_wy_aver_739_ad <- snotel_739_adjusted %>% 
  group_by(waterYear) %>% 
  mutate(aver_ann_temp_ad = mean(temp_ad))
#Average temperature by day for all water years:
daily_wy_aver_739_ad <- yearly_wy_aver_739_ad %>% 
  group_by(daymonth) %>% 
  mutate(aver_day_temp_ad = mean(aver_ann_temp_ad))
#average mean temperature by day for the period of record:
daily_wy_aver_739_ad <- daily_wy_aver_739_ad %>% 
  group_by(daymonth) %>% 
  mutate(all_ave_temp_ad = mean(daily_wy_aver_739_ad$aver_day_temp_ad))
# try to show all years as means. 
daily_wy_aver2_739_ad <-daily_wy_aver_739_ad %>% 
  group_by(waterDay) %>%
  mutate(date_temp_ad = mean(temp_ad))
  
daily_wy_aver2_739_ad$date_temp_ad <- signif(daily_wy_aver2_739_ad$date_temp_ad,3) #reduce the sig figs
ggplot(daily_wy_aver2_739_ad, aes(x = waterDay, y = date_temp_ad))+
  geom_line(size= 0.7) +
  theme_few() +
  ylab('Average Daily temperature (°C)') + 
  xlab('Day of water year')

739 SS (corrected)

standard_dev_739_ad <- daily_wy_aver_739_ad %>% 
  group_by(waterYear) %>% 
  #filter(waterYear >= 1987 & waterYear <= 2021) %>% 
  mutate(residual = (all_ave_temp_ad-aver_ann_temp_ad)+temp_ad-aver_day_temp_ad) %>% 
  mutate(deviation = abs(residual-lag(residual)))
standard_dev_all_739_ad <- standard_dev_739_ad %>% 
  group_by(waterYear) %>% 
  mutate(nmbr = n())
standard_dev_all_739_ad <- standard_dev_all_739_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
standard_dev_all_739_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1989 6.882254
1990 5.641380
1991 5.682802
1992 4.573743
1995 7.029026
1996 7.246834
1997 7.374205
1998 7.773434
1999 6.583515
2000 7.110733
2001 7.493196
2002 8.076937
2003 7.362576
2004 7.247292
2005 7.144310
2006 7.250207
2007 7.615515
2008 7.916383
2009 6.789140
2010 7.880400
2011 7.703781
2012 7.458914
2013 8.171585
2014 7.223615
2015 6.545250
2018 6.908860
2019 7.558520
2020 7.674026
2021 7.658936
2022 7.428362
ggplot(standard_dev_all_739_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 739 average temperatures for water years 1986-2021

MK & SS 739 (corrected)

sd_mk_739_ad <- mk.test(standard_dev_all_739_ad$sd_2)
print(sd_mk_739_ad)
## 
##  Mann-Kendall trend test
## 
## data:  standard_dev_all_739_ad$sd_2
## z = 2.3907, n = 30, p-value = 0.01682
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
##  135.0000000 3141.6666667    0.3103448
sd_sens_739_ad <- sens.slope(standard_dev_all_739_ad$sd_2)
print(sd_sens_739_ad)
## 
##  Sen's slope
## 
## data:  standard_dev_all_739_ad$sd_2
## z = 2.3907, n = 30, p-value = 0.01682
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  0.00806300 0.05860595
## sample estimates:
## Sen's slope 
##  0.02802846

Summer and Winter SD NOT-CORRECTED

Summer

summer_standard_dev_all_739 <- standard_dev_739 %>%
  filter(waterDay >= 244 & waterDay <= 335) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())

summer_standard_dev_all_739 <- summer_standard_dev_all_739 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

summer_standard_dev_all_739 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1989 2.536063
1990 2.091777
1991 1.899421
1992 2.191209
1995 2.938309
1996 1.887456
1997 1.912590
1998 2.809296
1999 2.529949
2000 1.932726
2001 2.352198
2002 2.220673
2003 2.464625
2004 2.327233
2005 2.646323
2006 1.792276
2007 2.092948
2008 2.102311
2009 2.386216
2010 2.100594
2011 1.840326
2012 1.559146
2013 1.939526
2014 1.846238
2015 2.036347
2018 1.620029
2019 2.139991
2020 1.951129
2021 1.888555
2022 1.816675
ggplot(summer_standard_dev_all_739, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 739 average summer temperatures for water years 2005-2021

summer MK & SS for 739 (non-corrected)

summer_sd_mk_739 <- mk.test(summer_standard_dev_all_739$sd_2)
print(summer_sd_mk_739)
## 
##  Mann-Kendall trend test
## 
## data:  summer_standard_dev_all_739$sd_2
## z = -2.4621, n = 30, p-value = 0.01381
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
## -139.0000000 3141.6666667   -0.3195402
summer_sd_sens_739 <- sens.slope(summer_standard_dev_all_739$sd_2)
print(summer_sd_sens_739)
## 
##  Sen's slope
## 
## data:  summer_standard_dev_all_739$sd_2
## z = -2.4621, n = 30, p-value = 0.01381
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.034389289 -0.003903077
## sample estimates:
## Sen's slope 
## -0.01943234

Winter

winter_standard_dev_all_739 <- standard_dev_739 %>%
  filter(waterDay >= 32 & waterDay <= 182) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())

winter_standard_dev_all_739 <- winter_standard_dev_all_739 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

winter_standard_dev_all_739 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1989 5.526982
1990 4.286471
1991 4.806519
1992 3.671502
1995 4.428531
1996 4.403085
1997 4.558376
1998 4.420439
1999 4.103676
2000 4.520720
2001 3.763356
2002 4.869553
2003 3.918235
2004 5.273369
2005 3.826935
2006 4.277452
2007 4.857620
2008 5.240319
2009 4.089971
2010 4.127199
2011 5.058688
2012 3.660887
2013 5.142996
2014 3.958382
2015 4.114433
2018 3.906291
2019 3.878282
2020 4.288826
2021 4.058320
2022 4.212817
ggplot(winter_standard_dev_all_739, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 739 average winter temperatures for water years 2005-2021

winter MK & SS for 739 (non-corrected)

winter_sd_mk_739 <- mk.test(winter_standard_dev_all_739$sd_2)
print(winter_sd_mk_739)
## 
##  Mann-Kendall trend test
## 
## data:  winter_standard_dev_all_739$sd_2
## z = -1.2846, n = 30, p-value = 0.1989
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
##  -73.0000000 3141.6666667   -0.1678161
winter_sd_sens_739 <- sens.slope(winter_standard_dev_all_739$sd_2)
print(winter_sd_sens_739)
## 
##  Sen's slope
## 
## data:  winter_standard_dev_all_739$sd_2
## z = -1.2846, n = 30, p-value = 0.1989
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.03347603  0.01067222
## sample estimates:
## Sen's slope 
## -0.01498978

Summer and Winter SD CORRECTED

Summer

summer_standard_dev_all_739_ad <- standard_dev_739_ad %>% 
  filter(waterDay >= 244 & waterDay <= 335) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())
summer_standard_dev_all_739_ad <- summer_standard_dev_all_739_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
summer_standard_dev_all_739_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1989 2.287694
1990 1.897615
1991 1.733673
1992 2.002649
1995 2.637660
1996 1.689280
1997 1.716626
1998 2.522226
1999 2.276814
2000 1.730206
2001 2.111149
2002 1.988276
2003 2.206426
2004 2.200327
2005 2.645890
2006 1.791669
2007 2.092764
2008 2.102071
2009 2.385848
2010 2.100675
2011 1.839896
2012 1.558492
2013 1.939878
2014 1.845489
2015 2.036358
2018 1.619529
2019 2.139593
2020 1.950178
2021 1.888439
2022 1.816671
ggplot(summer_standard_dev_all_739_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 739 average summer temperatures for water years 1986-2021

summer MK & SS 739 (corrected)

summer_sd_mk_739_ad <- mk.test(summer_standard_dev_all_739_ad$sd_2)
print(summer_sd_mk_739_ad)
## 
##  Mann-Kendall trend test
## 
## data:  summer_standard_dev_all_739_ad$sd_2
## z = -1.1775, n = 30, p-value = 0.239
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##           S        varS         tau 
##  -67.000000 3141.666667   -0.154023
summer_sd_sens_739_ad <- sens.slope(summer_standard_dev_all_739_ad$sd_2)
print(summer_sd_sens_739_ad)
## 
##  Sen's slope
## 
## data:  summer_standard_dev_all_739_ad$sd_2
## z = -1.1775, n = 30, p-value = 0.239
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.019874143  0.005901278
## sample estimates:
##  Sen's slope 
## -0.007567534

Winter

winter_standard_dev_all_739_ad <- standard_dev_739_ad %>% 
  filter(waterDay >= 32 & waterDay <= 182) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())
winter_standard_dev_all_739_ad <- winter_standard_dev_all_739_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
winter_standard_dev_all_739_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1989 5.349986
1990 4.111286
1991 4.682872
1992 3.514678
1995 4.239937
1996 4.226770
1997 4.372688
1998 4.241526
1999 3.922109
2000 4.321534
2001 3.609996
2002 4.664023
2003 3.760080
2004 5.057360
2005 3.826730
2006 4.278107
2007 4.857019
2008 5.239709
2009 4.090550
2010 4.127290
2011 5.058421
2012 3.661139
2013 5.143336
2014 3.958876
2015 4.114007
2018 3.906290
2019 3.878351
2020 4.288406
2021 4.058565
2022 4.213592
ggplot(winter_standard_dev_all_739_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 739 average winter temperatures for water years 1986-2021

winter MK & SS 739 (corrected)

winter_sd_mk_739_ad <- mk.test(winter_standard_dev_all_739_ad$sd_2)
print(winter_sd_mk_739_ad)
## 
##  Mann-Kendall trend test
## 
## data:  winter_standard_dev_all_739_ad$sd_2
## z = -0.49955, n = 30, p-value = 0.6174
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##             S          varS           tau 
##  -29.00000000 3141.66666667   -0.06666667
winter_sd_sens_739_ad <- sens.slope(winter_standard_dev_all_739_ad$sd_2)
print(winter_sd_sens_739_ad)
## 
##  Sen's slope
## 
## data:  winter_standard_dev_all_739_ad$sd_2
## z = -0.49955, n = 30, p-value = 0.6174
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.02434186  0.01865529
## sample estimates:
##  Sen's slope 
## -0.005934895

Slumgullion 762

Morrisey 6/26/2006

snotel_762 <- SNOTEL_san_juan_Area %>% 
  filter(site_id == "762")
#str(snotel_762) # check the date, usually a character.  

snotel_762$Date <- as.Date(snotel_762$date) #change date from character to date format, capitalize to work with Water year functon from NWIS.

#THIS WILL CHANGE FOR EACH STATION
snotel_762_clean <- snotel_762 %>% # filter for the timeframe
  filter(Date >= "1979-10-01" & Date <= "2022-09-30") %>%
  #filter(temperature_mean >= -30 & temperature_mean <= 20) %>% # removing outliers   
  addWaterYear() %>% 
  mutate(daymonth = format(as.Date(Date), "%d-%m")) %>% 
  na.omit()

#adding water day using difftime (SUPER COOL. example from [this](https://stackoverflow.com/questions/48123049/create-day-index-based-on-water-year))

snotel_762_clean <- snotel_762_clean %>% 
  group_by(waterYear)%>% 
  mutate(waterDay = (as.integer(difftime(Date, ymd(paste0(waterYear - 1 ,'-09-30')), units = "days"))))
# Check for outliers

ggplot(snotel_762_clean, aes(x = Date, y = temperature_mean)) +
  geom_point() + #lwd = 2) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature (°C)') + 
  xlab('Date')

snotel_762_clean <- snotel_762_clean %>% 
  mutate(temp_diff = abs(temperature_min - temperature_max)) %>% 
  filter(temperature_mean > -40) %>% 
  filter(temperature_mean < 25)
ggplot(snotel_762_clean, aes(x = Date, y = temp_diff)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  #geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature varience (°C)') + 
  xlab('Date')

Per Steven’s advice: :If there are more than 15 missing days, then…remove that year”

# filtering for temperature anomalies
#snotel_762_cull_count <- snotel_762_clean %>% 
#  filter(temperature_min > -40) %>% 
#  count(waterYear)

#snotel_762_cull_count

# filtering for too few observations in a year
snotel_762_cull_count_days <- snotel_762_clean %>% 
  group_by(waterYear) %>% 
  count(waterYear) %>% 
  filter(n < 350)

snotel_762_cull_count_days
## # A tibble: 5 x 2
## # Groups:   waterYear [5]
##   waterYear     n
##       <dbl> <int>
## 1      1980    43
## 2      1982     1
## 3      1983   319
## 4      1994   331
## 5      2003   337

1980, 1982, 1994, 2003 need to be culled. 1995 maximums are less than the minimums.

snotel_762_clean_culled <- snotel_762_clean %>% 
  filter(waterYear != "1980" & waterYear != "1981" & waterYear != "1982" & waterYear != "1983" & waterYear != "1994" & waterYear != "1995" & waterYear != "2003")# & waterYear != "2017" & waterYear != "1993" & waterYear != "1994" & waterYear != "2021" & waterYear != "2022") & waterYear != "2014") #%>% 
  #filter(temperature_mean > -49)

ggplot(snotel_762_clean_culled, aes(x = Date, y = temp_diff)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  #geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature varience (°C)') + 
  xlab('Date')

ggplot(snotel_762_clean_culled, aes(x = Date, y = temperature_mean)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature (°C)') + 
  xlab('Date')

temp_762_xts <- xts(snotel_762_clean_culled$temperature_mean, order.by = snotel_762_clean_culled$Date)

dygraph(temp_762_xts) %>%
  dyAxis("y", label = "Daily mean temperature (°C)") 
#snotel_762_clean_culled <- snotel_762_clean_culled %>% 
#  filter(temperature_mean < 19.3)

temp_762_xts <- xts(snotel_762_clean_culled$temperature_mean, order.by = snotel_762_clean_culled$Date)

dygraph(temp_762_xts) %>%
  dyAxis("y", label = "Daily mean temperature (°C)") 

Slumgullion 762

Morrisey 6/26/2006

snotel_762_adjusted <- snotel_762_clean_culled %>%
  mutate(temp_ad = if_else(Date < "2006-06-26", ((5.3*10^(-7))*(temperature_mean^(4))+(3.72*10^(-5))*(temperature_mean^(3))-(2.16*10^(-3))*(temperature_mean^(2))-(7.32*10^(-2))*(temperature_mean)+1.37)+temperature_mean, temperature_mean))

Non-corrected

762 Detrended

#using the clean culled df:

#average water year temperature

yearly_wy_aver_762 <- snotel_762_adjusted %>% 
  group_by(waterYear) %>% 
  mutate(aver_ann_temp = mean(temperature_mean))

#Average temperature by day for all water years:

daily_wy_aver_762 <- yearly_wy_aver_762 %>% 
  group_by(daymonth) %>% 
  mutate(aver_day_temp = mean(aver_ann_temp))

#average mean temperature by day for the period of record:

daily_wy_aver_762 <- daily_wy_aver_762 %>% 
  group_by(daymonth) %>% 
  mutate(all_ave_temp = mean(daily_wy_aver_762$aver_day_temp))

# try to show all years as means. 
daily_wy_aver2_762 <-daily_wy_aver_762 %>% 
  group_by(waterDay) %>%
  mutate(date_temp = mean(temperature_mean))
  

daily_wy_aver2_762$date_temp <- signif(daily_wy_aver2_762$date_temp,3) #reduce the sig figs


ggplot(daily_wy_aver2_762, aes(x = waterDay, y = date_temp))+
  geom_line(size= 0.7) +
  theme_few() +
  ylab('Average Daily temperature (°C)') + 
  xlab('Day of water year')

762 SD

standard_dev_762 <- daily_wy_aver_762 %>% 
  group_by(waterYear) %>% 
  mutate(residual = (all_ave_temp-aver_ann_temp)+temperature_mean-aver_day_temp) %>% 
  mutate(deviation = abs(residual-lag(residual)))

standard_dev_all_762 <- standard_dev_762 %>% 
  group_by(waterYear) %>% 
  mutate(nmbr = n())

standard_dev_all_762 <- standard_dev_all_762 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

standard_dev_all_762 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1984 8.474614
1985 8.395416
1986 7.224771
1987 8.036169
1988 8.470217
1989 8.556810
1990 8.104917
1991 8.129177
1992 7.378858
1993 7.881362
1996 8.151725
1997 8.002402
1998 8.492764
1999 7.140044
2000 7.859908
2001 8.456394
2002 8.777834
2004 8.172096
2005 8.157803
2006 8.479381
2007 7.832136
2008 8.378519
2009 7.328609
2010 8.218219
2011 8.186856
2012 7.846460
2013 8.493891
2014 7.772861
2015 7.080609
2016 7.803357
2017 7.532567
2018 7.423620
2019 8.010211
2020 8.142119
2021 8.245368
2022 7.614681
ggplot(standard_dev_all_762, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 762 average temperatures for water years 2005-2021

MK & SS for 762 (non-corrected)

sd_mk_762 <- mk.test(standard_dev_all_762$sd_2)
print(sd_mk_762)
## 
##  Mann-Kendall trend test
## 
## data:  standard_dev_all_762$sd_2
## z = -1.4574, n = 36, p-value = 0.145
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
## -108.0000000 5390.0000000   -0.1714286
sd_sens_762 <- sens.slope(standard_dev_all_762$sd_2)
print(sd_sens_762)
## 
##  Sen's slope
## 
## data:  standard_dev_all_762$sd_2
## z = -1.4574, n = 36, p-value = 0.145
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.024958596  0.002910121
## sample estimates:
## Sen's slope 
## -0.01112266

Corrected

762 Detrended (corrected)

#using the clean culled df:
#average water year temperature
yearly_wy_aver_762_ad <- snotel_762_adjusted %>% 
  group_by(waterYear) %>% 
  mutate(aver_ann_temp_ad = mean(temp_ad))
#Average temperature by day for all water years:
daily_wy_aver_762_ad <- yearly_wy_aver_762_ad %>% 
  group_by(daymonth) %>% 
  mutate(aver_day_temp_ad = mean(aver_ann_temp_ad))
#average mean temperature by day for the period of record:
daily_wy_aver_762_ad <- daily_wy_aver_762_ad %>% 
  group_by(daymonth) %>% 
  mutate(all_ave_temp_ad = mean(daily_wy_aver_762_ad$aver_day_temp_ad))
# try to show all years as means. 
daily_wy_aver2_762_ad <-daily_wy_aver_762_ad %>% 
  group_by(waterDay) %>%
  mutate(date_temp_ad = mean(temp_ad))
  
daily_wy_aver2_762_ad$date_temp_ad <- signif(daily_wy_aver2_762_ad$date_temp_ad,3) #reduce the sig figs
ggplot(daily_wy_aver2_762_ad, aes(x = waterDay, y = date_temp_ad))+
  geom_line(size= 0.7) +
  theme_few() +
  ylab('Average Daily temperature (°C)') + 
  xlab('Day of water year')

762 SS (corrected)

standard_dev_762_ad <- daily_wy_aver_762_ad %>% 
  group_by(waterYear) %>% 
  #filter(waterYear >= 1987 & waterYear <= 2021) %>% 
  mutate(residual = (all_ave_temp_ad-aver_ann_temp_ad)+temp_ad-aver_day_temp_ad) %>% 
  mutate(deviation = abs(residual-lag(residual)))
standard_dev_all_762_ad <- standard_dev_762_ad %>% 
  group_by(waterYear) %>% 
  mutate(nmbr = n())
standard_dev_all_762_ad <- standard_dev_all_762_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
standard_dev_all_762_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1984 7.970791
1985 7.888311
1986 6.764665
1987 7.528970
1988 7.965877
1989 8.053090
1990 7.604786
1991 7.665906
1992 6.904408
1993 7.400370
1996 7.629529
1997 7.511155
1998 7.956453
1999 6.688385
2000 7.353371
2001 7.919104
2002 8.206929
2004 7.685091
2005 7.619040
2006 7.823927
2007 7.831876
2008 8.379138
2009 7.329078
2010 8.218734
2011 8.187217
2012 7.846411
2013 8.493922
2014 7.773398
2015 7.081459
2016 7.804132
2017 7.532344
2018 7.424275
2019 8.010639
2020 8.142599
2021 8.245690
2022 7.615524
ggplot(standard_dev_all_762_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 762 average temperatures for water years 1986-2021

MK & SS 762 (corrected)

sd_mk_762_ad <- mk.test(standard_dev_all_762_ad$sd_2)
print(sd_mk_762_ad)
## 
##  Mann-Kendall trend test
## 
## data:  standard_dev_all_762_ad$sd_2
## z = 1.2123, n = 36, p-value = 0.2254
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
##   90.0000000 5390.0000000    0.1428571
sd_sens_762_ad <- sens.slope(standard_dev_all_762_ad$sd_2)
print(sd_sens_762_ad)
## 
##  Sen's slope
## 
## data:  standard_dev_all_762_ad$sd_2
## z = 1.2123, n = 36, p-value = 0.2254
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.00495755  0.02329061
## sample estimates:
## Sen's slope 
## 0.009103679

Summer and Winter SD NOT-CORRECTED

Summer

summer_standard_dev_all_762 <- standard_dev_762 %>%
  filter(waterDay >= 244 & waterDay <= 335) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())

summer_standard_dev_all_762 <- summer_standard_dev_all_762 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

summer_standard_dev_all_762 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1984 2.822167
1985 2.555079
1986 2.456898
1987 2.326799
1988 2.500087
1989 3.281463
1990 2.649819
1991 2.481917
1992 2.963695
1993 2.790144
1996 2.378361
1997 2.358193
1998 3.185997
1999 2.475766
2000 1.869817
2001 2.644390
2002 2.439828
2004 2.563353
2005 3.421058
2006 2.194534
2007 2.598497
2008 2.415739
2009 2.925849
2010 2.356057
2011 1.997263
2012 1.775514
2013 2.046112
2014 2.092032
2015 2.375980
2016 2.597941
2017 1.879397
2018 1.844924
2019 2.569473
2020 2.556016
2021 2.555837
2022 2.102396
ggplot(summer_standard_dev_all_762, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 762 average summer temperatures for water years 2005-2021

summer MK & SS for 762 (non-corrected)

summer_sd_mk_762 <- mk.test(summer_standard_dev_all_762$sd_2)
print(summer_sd_mk_762)
## 
##  Mann-Kendall trend test
## 
## data:  summer_standard_dev_all_762$sd_2
## z = -2.3019, n = 36, p-value = 0.02134
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
## -170.0000000 5390.0000000   -0.2698413
summer_sd_sens_762 <- sens.slope(summer_standard_dev_all_762$sd_2)
print(summer_sd_sens_762)
## 
##  Sen's slope
## 
## data:  summer_standard_dev_all_762$sd_2
## z = -2.3019, n = 36, p-value = 0.02134
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.025908824 -0.002255574
## sample estimates:
## Sen's slope 
##  -0.0135962

Winter

winter_standard_dev_all_762 <- standard_dev_762 %>%
  filter(waterDay >= 32 & waterDay <= 182) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())

winter_standard_dev_all_762 <- winter_standard_dev_all_762 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

winter_standard_dev_all_762 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1984 4.995368
1985 4.776362
1986 4.673415
1987 4.214252
1988 4.925869
1989 5.787831
1990 5.030413
1991 4.990405
1992 3.600871
1993 3.860851
1996 4.853103
1997 5.005292
1998 4.309774
1999 4.363109
2000 5.106424
2001 4.007137
2002 5.049274
2004 5.401251
2005 4.115976
2006 4.838876
2007 5.203133
2008 5.549026
2009 4.564164
2010 4.542645
2011 5.336582
2012 4.358678
2013 5.680888
2014 4.319649
2015 4.719867
2016 4.854215
2017 5.253700
2018 4.335060
2019 4.067880
2020 4.554385
2021 4.535441
2022 4.856251
ggplot(winter_standard_dev_all_762, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 762 average winter temperatures for water years 2005-2021

winter MK & SS for 762 (non-corrected)

winter_sd_mk_762 <- mk.test(winter_standard_dev_all_762$sd_2)
print(winter_sd_mk_762)
## 
##  Mann-Kendall trend test
## 
## data:  winter_standard_dev_all_762$sd_2
## z = -0.040863, n = 36, p-value = 0.9674
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##             S          varS           tau 
## -4.000000e+00  5.390000e+03 -6.349206e-03
winter_sd_sens_762 <- sens.slope(winter_standard_dev_all_762$sd_2)
print(winter_sd_sens_762)
## 
##  Sen's slope
## 
## data:  winter_standard_dev_all_762$sd_2
## z = -0.040863, n = 36, p-value = 0.9674
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.01894413  0.02022456
## sample estimates:
##  Sen's slope 
## -0.000681984

Summer and Winter SD CORRECTED

Summer

summer_standard_dev_all_762_ad <- standard_dev_762_ad %>% 
  filter(waterDay >= 244 & waterDay <= 335) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())
summer_standard_dev_all_762_ad <- summer_standard_dev_all_762_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
summer_standard_dev_all_762_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1984 2.557710
1985 2.300170
1986 2.215330
1987 2.094745
1988 2.264840
1989 2.952743
1990 2.389207
1991 2.244600
1992 2.680413
1993 2.530155
1996 2.140008
1997 2.129328
1998 2.878370
1999 2.244239
2000 1.680367
2001 2.391753
2002 2.192942
2004 2.311148
2005 3.087189
2006 2.095769
2007 2.597803
2008 2.416464
2009 2.927464
2010 2.358119
2011 1.998743
2012 1.774917
2013 2.045463
2014 2.095539
2015 2.376063
2016 2.599228
2017 1.878151
2018 1.846389
2019 2.572313
2020 2.556578
2021 2.556287
2022 2.107440
ggplot(summer_standard_dev_all_762_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 762 average summer temperatures for water years 1986-2021

summer MK & SS 762 (corrected)

summer_sd_mk_762_ad <- mk.test(summer_standard_dev_all_762_ad$sd_2)
print(summer_sd_mk_762_ad)
## 
##  Mann-Kendall trend test
## 
## data:  summer_standard_dev_all_762_ad$sd_2
## z = -0.77639, n = 36, p-value = 0.4375
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##             S          varS           tau 
##  -58.00000000 5390.00000000   -0.09206349
summer_sd_sens_762_ad <- sens.slope(summer_standard_dev_all_762_ad$sd_2)
print(summer_sd_sens_762_ad)
## 
##  Sen's slope
## 
## data:  summer_standard_dev_all_762_ad$sd_2
## z = -0.77639, n = 36, p-value = 0.4375
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.01464010  0.00776112
## sample estimates:
##  Sen's slope 
## -0.004782858

Winter

winter_standard_dev_all_762_ad <- standard_dev_762_ad %>% 
  filter(waterDay >= 32 & waterDay <= 182) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())
winter_standard_dev_all_762_ad <- winter_standard_dev_all_762_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
winter_standard_dev_all_762_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1984 4.887027
1985 4.720205
1986 4.524947
1987 4.099691
1988 4.817354
1989 5.629118
1990 4.889271
1991 4.904495
1992 3.496221
1993 3.767984
1996 4.729201
1997 4.843802
1998 4.197520
1999 4.230043
2000 4.928233
2001 3.915699
2002 4.894103
2004 5.256656
2005 4.014931
2006 4.718241
2007 5.202411
2008 5.548451
2009 4.564190
2010 4.542164
2011 5.336551
2012 4.358113
2013 5.680945
2014 4.320050
2015 4.719525
2016 4.853884
2017 5.252630
2018 4.334773
2019 4.067965
2020 4.553878
2021 4.535578
2022 4.855907
ggplot(winter_standard_dev_all_762_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 762 average winter temperatures for water years 1986-2021

winter MK & SS 762 (corrected)

winter_sd_mk_762_ad <- mk.test(winter_standard_dev_all_762_ad$sd_2)
print(winter_sd_mk_762_ad)
## 
##  Mann-Kendall trend test
## 
## data:  winter_standard_dev_all_762_ad$sd_2
## z = 0.28604, n = 36, p-value = 0.7748
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
## 2.200000e+01 5.390000e+03 3.492063e-02
winter_sd_sens_762_ad <- sens.slope(winter_standard_dev_all_762_ad$sd_2)
print(winter_sd_sens_762_ad)
## 
##  Sen's slope
## 
## data:  winter_standard_dev_all_762_ad$sd_2
## z = 0.28604, n = 36, p-value = 0.7748
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.01448371  0.02461905
## sample estimates:
## Sen's slope 
## 0.001085455

Spud Mountain 780

Morrisey 6/28/2004

snotel_780 <- SNOTEL_san_juan_Area %>% 
  filter(site_id == "780")
#str(snotel_780) # check the date, usually a character.  

snotel_780$Date <- as.Date(snotel_780$date) #change date from character to date format, capitalize to work with Water year functon from NWIS.

#THIS WILL CHANGE FOR EACH STATION
snotel_780_clean <- snotel_780 %>% # filter for the timeframe
  filter(Date >= "1979-10-01" & Date <= "2022-09-30") %>%
  #filter(temperature_mean >= -30 & temperature_mean <= 20) %>% # removing outliers   
  addWaterYear() %>% 
  mutate(daymonth = format(as.Date(Date), "%d-%m")) %>% 
  na.omit()

#adding water day using difftime (SUPER COOL. example from [this](https://stackoverflow.com/questions/48123049/create-day-index-based-on-water-year))

snotel_780_clean <- snotel_780_clean %>% 
  group_by(waterYear)%>% 
  mutate(waterDay = (as.integer(difftime(Date, ymd(paste0(waterYear - 1 ,'-09-30')), units = "days"))))
# Check for outliers

ggplot(snotel_780_clean, aes(x = Date, y = temperature_mean)) +
  geom_point() + #lwd = 2) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature (°C)') + 
  xlab('Date')

snotel_780_clean <- snotel_780_clean %>% 
  mutate(temp_diff = abs(temperature_min - temperature_max)) %>% 
  filter(temperature_mean > -40) %>% 
  filter(temperature_mean < 25)
ggplot(snotel_780_clean, aes(x = Date, y = temp_diff)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  #geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature varience (°C)') + 
  xlab('Date')

Per Steven’s advice: :If there are more than 15 missing days, then…remove that year”

# filtering for temperature anomalies
#snotel_780_cull_count <- snotel_780_clean %>% 
#  filter(temperature_min > -40) %>% 
#  count(waterYear)

#snotel_780_cull_count

# filtering for too few observations in a year
snotel_780_cull_count_days <- snotel_780_clean %>% 
  group_by(waterYear) %>% 
  count(waterYear) %>% 
  filter(n < 350)

snotel_780_cull_count_days
## # A tibble: 8 x 2
## # Groups:   waterYear [8]
##   waterYear     n
##       <dbl> <int>
## 1      1987   315
## 2      1994   336
## 3      1998   309
## 4      2009   342
## 5      2011   337
## 6      2013   336
## 7      2016   331
## 8      2017   324

1987, 1994, 1998, 2009, 2011, 2013, 2016, 2017 need to be culled.

snotel_780_clean_culled <- snotel_780_clean %>% 
  filter(waterYear != "1987" & waterYear != "1994" & waterYear != "1998" & waterYear != "2009" & waterYear != "2011" & waterYear != "2013" & waterYear != "2016" & waterYear != "2017")# & waterYear != "1993" & waterYear != "1994" & waterYear != "2021" & waterYear != "2022") & waterYear != "2014") #%>% 
  #filter(temperature_mean > -49)

ggplot(snotel_780_clean_culled, aes(x = Date, y = temp_diff)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  #geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature varience (°C)') + 
  xlab('Date')

ggplot(snotel_780_clean_culled, aes(x = Date, y = temperature_mean)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature (°C)') + 
  xlab('Date')

temp_780_xts <- xts(snotel_780_clean_culled$temperature_mean, order.by = snotel_780_clean_culled$Date)

dygraph(temp_780_xts) %>%
  dyAxis("y", label = "Daily mean temperature (°C)") 
#snotel_780_clean_culled <- snotel_780_clean_culled %>% 
#  filter(temperature_mean < 19.3)

temp_780_xts <- xts(snotel_780_clean_culled$temperature_mean, order.by = snotel_780_clean_culled$Date)

dygraph(temp_780_xts) %>%
  dyAxis("y", label = "Daily mean temperature (°C)") 

Spud Mountain 780

Morrisey 6/28/2004

snotel_780_adjusted <- snotel_780_clean_culled %>%
  mutate(temp_ad = if_else(Date < "2004-06-28", ((5.3*10^(-7))*(temperature_mean^(4))+(3.72*10^(-5))*(temperature_mean^(3))-(2.16*10^(-3))*(temperature_mean^(2))-(7.32*10^(-2))*(temperature_mean)+1.37)+temperature_mean, temperature_mean))

Non-corrected

780 Detrended

#using the clean culled df:

#average water year temperature

yearly_wy_aver_780 <- snotel_780_adjusted %>% 
  group_by(waterYear) %>% 
  mutate(aver_ann_temp = mean(temperature_mean))

#Average temperature by day for all water years:

daily_wy_aver_780 <- yearly_wy_aver_780 %>% 
  group_by(daymonth) %>% 
  mutate(aver_day_temp = mean(aver_ann_temp))

#average mean temperature by day for the period of record:

daily_wy_aver_780 <- daily_wy_aver_780 %>% 
  group_by(daymonth) %>% 
  mutate(all_ave_temp = mean(daily_wy_aver_780$aver_day_temp))

# try to show all years as means. 
daily_wy_aver2_780 <-daily_wy_aver_780 %>% 
  group_by(waterDay) %>%
  mutate(date_temp = mean(temperature_mean))
  

daily_wy_aver2_780$date_temp <- signif(daily_wy_aver2_780$date_temp,3) #reduce the sig figs


ggplot(daily_wy_aver2_780, aes(x = waterDay, y = date_temp))+
  geom_line(size= 0.7) +
  theme_few() +
  ylab('Average Daily temperature (°C)') + 
  xlab('Day of water year')

780 SD

standard_dev_780 <- daily_wy_aver_780 %>% 
  group_by(waterYear) %>% 
  mutate(residual = (all_ave_temp-aver_ann_temp)+temperature_mean-aver_day_temp) %>% 
  mutate(deviation = abs(residual-lag(residual)))

standard_dev_all_780 <- standard_dev_780 %>% 
  group_by(waterYear) %>% 
  mutate(nmbr = n())

standard_dev_all_780 <- standard_dev_all_780 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

standard_dev_all_780 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1988 8.083874
1989 8.173292
1990 7.631378
1991 7.796215
1992 7.011676
1993 7.893616
1995 7.478626
1996 7.773665
1997 7.856295
1999 6.828980
2000 7.722333
2001 8.147987
2002 8.545717
2003 7.955225
2004 7.944675
2005 7.146399
2006 7.088438
2007 7.424508
2008 7.859459
2010 7.733705
2012 7.461301
2014 7.143839
2015 6.533563
2018 6.864656
2019 7.580081
2020 7.727076
2021 7.724978
2022 7.146411
ggplot(standard_dev_all_780, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 780 average temperatures for water years 2005-2021

MK & SS for 780 (non-corrected)

sd_mk_780 <- mk.test(standard_dev_all_780$sd_2)
print(sd_mk_780)
## 
##  Mann-Kendall trend test
## 
## data:  standard_dev_all_780$sd_2
## z = -1.9954, n = 28, p-value = 0.046
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
## -102.0000000 2562.0000000   -0.2698413
sd_sens_780 <- sens.slope(standard_dev_all_780$sd_2)
print(sd_sens_780)
## 
##  Sen's slope
## 
## data:  standard_dev_all_780$sd_2
## z = -1.9954, n = 28, p-value = 0.046
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.0394954014 -0.0004267439
## sample estimates:
## Sen's slope 
## -0.01908281

Corrected

780 Detrended (corrected)

#using the clean culled df:
#average water year temperature
yearly_wy_aver_780_ad <- snotel_780_adjusted %>% 
  group_by(waterYear) %>% 
  mutate(aver_ann_temp_ad = mean(temp_ad))
#Average temperature by day for all water years:
daily_wy_aver_780_ad <- yearly_wy_aver_780_ad %>% 
  group_by(daymonth) %>% 
  mutate(aver_day_temp_ad = mean(aver_ann_temp_ad))
#average mean temperature by day for the period of record:
daily_wy_aver_780_ad <- daily_wy_aver_780_ad %>% 
  group_by(daymonth) %>% 
  mutate(all_ave_temp_ad = mean(daily_wy_aver_780_ad$aver_day_temp_ad))
# try to show all years as means. 
daily_wy_aver2_780_ad <-daily_wy_aver_780_ad %>% 
  group_by(waterDay) %>%
  mutate(date_temp_ad = mean(temp_ad))
  
daily_wy_aver2_780_ad$date_temp_ad <- signif(daily_wy_aver2_780_ad$date_temp_ad,3) #reduce the sig figs
ggplot(daily_wy_aver2_780_ad, aes(x = waterDay, y = date_temp_ad))+
  geom_line(size= 0.7) +
  theme_few() +
  ylab('Average Daily temperature (°C)') + 
  xlab('Day of water year')

780 SS (corrected)

standard_dev_780_ad <- daily_wy_aver_780_ad %>% 
  group_by(waterYear) %>% 
  #filter(waterYear >= 1987 & waterYear <= 2021) %>% 
  mutate(residual = (all_ave_temp_ad-aver_ann_temp_ad)+temp_ad-aver_day_temp_ad) %>% 
  mutate(deviation = abs(residual-lag(residual)))
standard_dev_all_780_ad <- standard_dev_780_ad %>% 
  group_by(waterYear) %>% 
  mutate(nmbr = n())
standard_dev_all_780_ad <- standard_dev_all_780_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
standard_dev_all_780_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1988 7.530892
1989 7.629513
1990 7.085085
1991 7.275264
1992 6.494536
1993 7.336765
1995 6.928211
1996 7.209777
1997 7.307706
1999 6.331029
2000 7.141268
2001 7.555861
2002 7.925084
2003 7.356618
2004 7.299749
2005 7.146735
2006 7.089360
2007 7.425654
2008 7.860535
2010 7.734756
2012 7.462985
2014 7.144698
2015 6.534943
2018 6.866003
2019 7.581136
2020 7.728271
2021 7.725902
2022 7.147773
ggplot(standard_dev_all_780_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 780 average temperatures for water years 1986-2021

MK & SS 780 (corrected)

sd_mk_780_ad <- mk.test(standard_dev_all_780_ad$sd_2)
print(sd_mk_780_ad)
## 
##  Mann-Kendall trend test
## 
## data:  standard_dev_all_780_ad$sd_2
## z = 0.84953, n = 28, p-value = 0.3956
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
##   44.0000000 2562.0000000    0.1164021
sd_sens_780_ad <- sens.slope(standard_dev_all_780_ad$sd_2)
print(sd_sens_780_ad)
## 
##  Sen's slope
## 
## data:  standard_dev_all_780_ad$sd_2
## z = 0.84953, n = 28, p-value = 0.3956
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.01274116  0.02720599
## sample estimates:
## Sen's slope 
## 0.007453894

Summer and Winter SD NOT-CORRECTED

Summer

summer_standard_dev_all_780 <- standard_dev_780 %>%
  filter(waterDay >= 244 & waterDay <= 335) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())

summer_standard_dev_all_780 <- summer_standard_dev_all_780 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

summer_standard_dev_all_780 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1988 2.432792
1989 2.950137
1990 2.664522
1991 2.411156
1992 2.824120
1993 3.318665
1995 3.390624
1996 2.250871
1997 2.379508
1999 2.724005
2000 1.835908
2001 2.489843
2002 2.344072
2003 2.822363
2004 2.588494
2005 3.022099
2006 2.053504
2007 2.434362
2008 2.304875
2010 2.306507
2012 1.543810
2014 1.989689
2015 2.332147
2018 1.813695
2019 2.431221
2020 2.452038
2021 2.236513
2022 2.015161
ggplot(summer_standard_dev_all_780, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 780 average summer temperatures for water years 2005-2021

summer MK & SS for 780 (non-corrected)

summer_sd_mk_780 <- mk.test(summer_standard_dev_all_780$sd_2)
print(summer_sd_mk_780)
## 
##  Mann-Kendall trend test
## 
## data:  summer_standard_dev_all_780$sd_2
## z = -2.7462, n = 28, p-value = 0.00603
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
## -140.0000000 2562.0000000   -0.3703704
summer_sd_sens_780 <- sens.slope(summer_standard_dev_all_780$sd_2)
print(summer_sd_sens_780)
## 
##  Sen's slope
## 
## data:  summer_standard_dev_all_780$sd_2
## z = -2.7462, n = 28, p-value = 0.00603
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.044102535 -0.006660488
## sample estimates:
## Sen's slope 
## -0.02494145

Winter

winter_standard_dev_all_780 <- standard_dev_780 %>%
  filter(waterDay >= 32 & waterDay <= 182) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())

winter_standard_dev_all_780 <- winter_standard_dev_all_780 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

winter_standard_dev_all_780 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1988 4.728111
1989 5.529818
1990 4.915755
1991 4.625673
1992 3.366642
1993 3.857739
1995 4.159659
1996 4.941637
1997 4.864556
1999 4.008686
2000 4.800600
2001 3.896151
2002 5.030428
2003 4.105646
2004 5.103992
2005 3.891688
2006 4.388333
2007 5.052493
2008 5.353871
2010 4.321493
2012 4.136622
2014 4.085958
2015 4.487931
2018 4.168824
2019 3.905352
2020 4.270359
2021 4.213932
2022 4.574223
ggplot(winter_standard_dev_all_780, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 780 average winter temperatures for water years 2005-2021

winter MK & SS for 780 (non-corrected)

winter_sd_mk_780 <- mk.test(winter_standard_dev_all_780$sd_2)
print(winter_sd_mk_780)
## 
##  Mann-Kendall trend test
## 
## data:  winter_standard_dev_all_780$sd_2
## z = -0.53343, n = 28, p-value = 0.5937
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##             S          varS           tau 
##  -28.00000000 2562.00000000   -0.07407407
winter_sd_sens_780 <- sens.slope(winter_standard_dev_all_780$sd_2)
print(winter_sd_sens_780)
## 
##  Sen's slope
## 
## data:  winter_standard_dev_all_780$sd_2
## z = -0.53343, n = 28, p-value = 0.5937
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.03816715  0.01859223
## sample estimates:
## Sen's slope 
## -0.01321226

Summer and Winter SD CORRECTED

Summer

summer_standard_dev_all_780_ad <- standard_dev_780_ad %>% 
  filter(waterDay >= 244 & waterDay <= 335) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())
summer_standard_dev_all_780_ad <- summer_standard_dev_all_780_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
summer_standard_dev_all_780_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1988 2.192241
1989 2.645295
1990 2.393290
1991 2.174228
1992 2.544730
1993 2.991736
1995 3.050477
1996 2.016136
1997 2.142284
1999 2.459696
2000 1.644421
2001 2.239012
2002 2.097917
2003 2.527899
2004 2.423734
2005 3.021150
2006 2.051951
2007 2.434476
2008 2.305532
2010 2.304951
2012 1.543208
2014 1.987540
2015 2.333379
2018 1.813470
2019 2.429766
2020 2.452190
2021 2.237222
2022 2.014534
ggplot(summer_standard_dev_all_780_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 780 average summer temperatures for water years 1986-2021

summer MK & SS 780 (corrected)

summer_sd_mk_780_ad <- mk.test(summer_standard_dev_all_780_ad$sd_2)
print(summer_sd_mk_780_ad)
## 
##  Mann-Kendall trend test
## 
## data:  summer_standard_dev_all_780_ad$sd_2
## z = -1.4422, n = 28, p-value = 0.1492
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
##  -74.0000000 2562.0000000   -0.1957672
summer_sd_sens_780_ad <- sens.slope(summer_standard_dev_all_780_ad$sd_2)
print(summer_sd_sens_780_ad)
## 
##  Sen's slope
## 
## data:  summer_standard_dev_all_780_ad$sd_2
## z = -1.4422, n = 28, p-value = 0.1492
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.026977267  0.004499003
## sample estimates:
##  Sen's slope 
## -0.009732141

Winter

winter_standard_dev_all_780_ad <- standard_dev_780_ad %>% 
  filter(waterDay >= 32 & waterDay <= 182) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())
winter_standard_dev_all_780_ad <- winter_standard_dev_all_780_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
winter_standard_dev_all_780_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1988 4.558562
1989 5.338146
1990 4.702000
1991 4.482169
1992 3.217724
1993 3.722134
1995 3.982988
1996 4.735689
1997 4.662361
1999 3.836986
2000 4.561800
2001 3.751725
2002 4.818090
2003 3.939896
2004 4.897779
2005 3.891135
2006 4.388210
2007 5.053531
2008 5.353295
2010 4.321866
2012 4.138875
2014 4.086817
2015 4.488516
2018 4.170037
2019 3.905450
2020 4.270195
2021 4.213896
2022 4.574664
ggplot(winter_standard_dev_all_780_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 780 average winter temperatures for water years 1986-2021

winter MK & SS 780 (corrected)

winter_sd_mk_780_ad <- mk.test(winter_standard_dev_all_780_ad$sd_2)
print(winter_sd_mk_780_ad)
## 
##  Mann-Kendall trend test
## 
## data:  winter_standard_dev_all_780_ad$sd_2
## z = -0.05927, n = 28, p-value = 0.9527
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##             S          varS           tau 
##   -4.00000000 2562.00000000   -0.01058201
winter_sd_sens_780_ad <- sens.slope(winter_standard_dev_all_780_ad$sd_2)
print(winter_sd_sens_780_ad)
## 
##  Sen's slope
## 
## data:  winter_standard_dev_all_780_ad$sd_2
## z = -0.05927, n = 28, p-value = 0.9527
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.0289316  0.0270756
## sample estimates:
##  Sen's slope 
## -0.003745777

Stump Lakes 797

Morrisey 7/22/2005

snotel_797 <- SNOTEL_san_juan_Area %>% 
  filter(site_id == "797")
#str(snotel_797) # check the date, usually a character.  

snotel_797$Date <- as.Date(snotel_797$date) #change date from character to date format, capitalize to work with Water year functon from NWIS.

#THIS WILL CHANGE FOR EACH STATION
snotel_797_clean <- snotel_797 %>% # filter for the timeframe
  filter(Date >= "1979-10-01" & Date <= "2022-09-30") %>%
  #filter(temperature_mean >= -30 & temperature_mean <= 20) %>% # removing outliers   
  addWaterYear() %>% 
  mutate(daymonth = format(as.Date(Date), "%d-%m")) %>% 
  na.omit()

#adding water day using difftime (SUPER COOL. example from [this](https://stackoverflow.com/questions/48123049/create-day-index-based-on-water-year))

snotel_797_clean <- snotel_797_clean %>% 
  group_by(waterYear)%>% 
  mutate(waterDay = (as.integer(difftime(Date, ymd(paste0(waterYear - 1 ,'-09-30')), units = "days"))))
# Check for outliers

ggplot(snotel_797_clean, aes(x = Date, y = temperature_mean)) +
  geom_point() + #lwd = 2) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature (°C)') + 
  xlab('Date')

snotel_797_clean <- snotel_797_clean %>% 
  mutate(temp_diff = abs(temperature_min - temperature_max)) %>% 
  filter(temperature_mean > -40) %>% 
  filter(temperature_mean < 40)
ggplot(snotel_797_clean, aes(x = Date, y = temp_diff)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  #geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature varience (°C)') + 
  xlab('Date')

Per Steven’s advice: :If there are more than 15 missing days, then…remove that year”

# filtering for temperature anomalies
#snotel_797_cull_count <- snotel_797_clean %>% 
#  filter(temperature_min > -40) %>% 
#  count(waterYear)

#snotel_797_cull_count

# filtering for too few observations in a year
snotel_797_cull_count_days <- snotel_797_clean %>% 
  group_by(waterYear) %>% 
  count(waterYear) %>% 
  filter(n < 350)

snotel_797_cull_count_days
## # A tibble: 5 x 2
## # Groups:   waterYear [5]
##   waterYear     n
##       <dbl> <int>
## 1      1987   267
## 2      1993   320
## 3      1994   274
## 4      2005   329
## 5      2006   327

1987, 1993, 1994, 2005, 2006 need to be culled.

snotel_797_clean_culled <- snotel_797_clean %>% 
  filter(waterYear != "1994" & waterYear != "2005" & waterYear != "2006" & waterYear != "1993" & waterYear != "1987")# & waterYear != "2021" & waterYear != "2022") & waterYear != "2014") #%>% 
  #filter(temperature_mean > -49)

ggplot(snotel_797_clean_culled, aes(x = Date, y = temp_diff)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  #geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature varience (°C)') + 
  xlab('Date')

ggplot(snotel_797_clean_culled, aes(x = Date, y = temperature_mean)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature (°C)') + 
  xlab('Date')

temp_797_xts <- xts(snotel_797_clean_culled$temperature_mean, order.by = snotel_797_clean_culled$Date)

dygraph(temp_797_xts) %>%
  dyAxis("y", label = "Daily mean temperature (°C)") 
#snotel_797_clean_culled <- snotel_797_clean_culled %>% 
#  filter(temperature_mean < 19.3)

temp_797_xts <- xts(snotel_797_clean_culled$temperature_mean, order.by = snotel_797_clean_culled$Date)

dygraph(temp_797_xts) %>%
  dyAxis("y", label = "Daily mean temperature (°C)") 

Stump Lakes 797

Morrisey 7/22/2005

snotel_797_adjusted <- snotel_797_clean_culled %>%
  mutate(temp_ad = if_else(Date < "2005-07-22", ((5.3*10^(-7))*(temperature_mean^(4))+(3.72*10^(-5))*(temperature_mean^(3))-(2.16*10^(-3))*(temperature_mean^(2))-(7.32*10^(-2))*(temperature_mean)+1.37)+temperature_mean, temperature_mean))

Non-corrected

797 Detrended

#using the clean culled df:

#average water year temperature

yearly_wy_aver_797 <- snotel_797_adjusted %>% 
  group_by(waterYear) %>% 
  mutate(aver_ann_temp = mean(temperature_mean))

#Average temperature by day for all water years:

daily_wy_aver_797 <- yearly_wy_aver_797 %>% 
  group_by(daymonth) %>% 
  mutate(aver_day_temp = mean(aver_ann_temp))

#average mean temperature by day for the period of record:

daily_wy_aver_797 <- daily_wy_aver_797 %>% 
  group_by(daymonth) %>% 
  mutate(all_ave_temp = mean(daily_wy_aver_797$aver_day_temp))

# try to show all years as means. 
daily_wy_aver2_797 <-daily_wy_aver_797 %>% 
  group_by(waterDay) %>%
  mutate(date_temp = mean(temperature_mean))
  

daily_wy_aver2_797$date_temp <- signif(daily_wy_aver2_797$date_temp,3) #reduce the sig figs


ggplot(daily_wy_aver2_797, aes(x = waterDay, y = date_temp))+
  geom_line(size= 0.7) +
  theme_few() +
  ylab('Average Daily temperature (°C)') + 
  xlab('Day of water year')

797 SD

standard_dev_797 <- daily_wy_aver_797 %>% 
  group_by(waterYear) %>% 
  mutate(residual = (all_ave_temp-aver_ann_temp)+temperature_mean-aver_day_temp) %>% 
  mutate(deviation = abs(residual-lag(residual)))

standard_dev_all_797 <- standard_dev_797 %>% 
  group_by(waterYear) %>% 
  mutate(nmbr = n())

standard_dev_all_797 <- standard_dev_all_797 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

standard_dev_all_797 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1988 8.133643
1989 10.300128
1990 7.808751
1991 9.471092
1992 7.312520
1995 7.694636
1996 7.953496
1997 8.069085
1998 8.367447
1999 7.015052
2000 7.935172
2001 8.545423
2002 9.270169
2003 8.084230
2004 9.099232
2007 7.603452
2008 8.008843
2009 7.184718
2010 7.913867
2011 7.886632
2012 7.696409
2013 8.096842
2014 7.327713
2015 6.737528
2016 7.565177
2017 7.316786
2018 7.076498
2019 7.800277
2020 7.872608
2021 7.863889
2022 7.341735
ggplot(standard_dev_all_797, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 797 average temperatures for water years 2005-2021

MK & SS for 797 (non-corrected)

sd_mk_797 <- mk.test(standard_dev_all_797$sd_2)
print(sd_mk_797)
## 
##  Mann-Kendall trend test
## 
## data:  standard_dev_all_797$sd_2
## z = -2.5495, n = 31, p-value = 0.01079
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
## -151.0000000 3461.6666667   -0.3247312
sd_sens_797 <- sens.slope(standard_dev_all_797$sd_2)
print(sd_sens_797)
## 
##  Sen's slope
## 
## data:  standard_dev_all_797$sd_2
## z = -2.5495, n = 31, p-value = 0.01079
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.055919889 -0.005393295
## sample estimates:
## Sen's slope 
## -0.02911914

Corrected

797 Detrended (corrected)

#using the clean culled df:
#average water year temperature
yearly_wy_aver_797_ad <- snotel_797_adjusted %>% 
  group_by(waterYear) %>% 
  mutate(aver_ann_temp_ad = mean(temp_ad))
#Average temperature by day for all water years:
daily_wy_aver_797_ad <- yearly_wy_aver_797_ad %>% 
  group_by(daymonth) %>% 
  mutate(aver_day_temp_ad = mean(aver_ann_temp_ad))
#average mean temperature by day for the period of record:
daily_wy_aver_797_ad <- daily_wy_aver_797_ad %>% 
  group_by(daymonth) %>% 
  mutate(all_ave_temp_ad = mean(daily_wy_aver_797_ad$aver_day_temp_ad))
# try to show all years as means. 
daily_wy_aver2_797_ad <-daily_wy_aver_797_ad %>% 
  group_by(waterDay) %>%
  mutate(date_temp_ad = mean(temp_ad))
  
daily_wy_aver2_797_ad$date_temp_ad <- signif(daily_wy_aver2_797_ad$date_temp_ad,3) #reduce the sig figs
ggplot(daily_wy_aver2_797_ad, aes(x = waterDay, y = date_temp_ad))+
  geom_line(size= 0.7) +
  theme_few() +
  ylab('Average Daily temperature (°C)') + 
  xlab('Day of water year')

797 SS (corrected)

standard_dev_797_ad <- daily_wy_aver_797_ad %>% 
  group_by(waterYear) %>% 
  #filter(waterYear >= 1987 & waterYear <= 2021) %>% 
  mutate(residual = (all_ave_temp_ad-aver_ann_temp_ad)+temp_ad-aver_day_temp_ad) %>% 
  mutate(deviation = abs(residual-lag(residual)))
standard_dev_all_797_ad <- standard_dev_797_ad %>% 
  group_by(waterYear) %>% 
  mutate(nmbr = n())
standard_dev_all_797_ad <- standard_dev_all_797_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
standard_dev_all_797_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1988 7.629765
1989 9.567031
1990 7.287673
1991 8.816683
1992 6.729102
1995 7.165683
1996 7.422437
1997 7.545973
1998 7.813938
1999 6.542177
2000 7.377392
2001 7.978790
2002 8.614161
2003 7.487087
2004 8.402698
2007 7.603448
2008 8.008551
2009 7.184680
2010 7.914300
2011 7.886697
2012 7.696830
2013 8.096444
2014 7.327715
2015 6.737458
2016 7.565155
2017 7.316299
2018 7.076457
2019 7.800532
2020 7.873011
2021 7.863666
2022 7.341402
ggplot(standard_dev_all_797_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 797 average temperatures for water years 1986-2021

MK & SS 797 (corrected)

sd_mk_797_ad <- mk.test(standard_dev_all_797_ad$sd_2)
print(sd_mk_797_ad)
## 
##  Mann-Kendall trend test
## 
## data:  standard_dev_all_797_ad$sd_2
## z = -0.30594, n = 31, p-value = 0.7597
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##             S          varS           tau 
##  -19.00000000 3461.66666667   -0.04086022
sd_sens_797_ad <- sens.slope(standard_dev_all_797_ad$sd_2)
print(sd_sens_797_ad)
## 
##  Sen's slope
## 
## data:  standard_dev_all_797_ad$sd_2
## z = -0.30594, n = 31, p-value = 0.7597
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.03291859  0.02011237
## sample estimates:
## Sen's slope 
## -0.00412891

Summer and Winter SD NOT-CORRECTED

Summer

summer_standard_dev_all_797 <- standard_dev_797 %>%
  filter(waterDay >= 244 & waterDay <= 335) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())

summer_standard_dev_all_797 <- summer_standard_dev_all_797 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

summer_standard_dev_all_797 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1988 2.483579
1989 6.242646
1990 3.152975
1991 5.429548
1992 2.710330
1995 3.599372
1996 2.265251
1997 2.523495
1998 3.270027
1999 2.702494
2000 1.841536
2001 2.374428
2002 4.011811
2003 3.002708
2004 4.617372
2007 2.509460
2008 2.392826
2009 3.033721
2010 2.406375
2011 1.963297
2012 1.651274
2013 1.822955
2014 1.971373
2015 2.430667
2016 2.573683
2017 2.033856
2018 1.929844
2019 2.568367
2020 2.416969
2021 2.211379
2022 2.240925
ggplot(summer_standard_dev_all_797, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 797 average summer temperatures for water years 2005-2021

summer MK & SS for 797 (non-corrected)

summer_sd_mk_797 <- mk.test(summer_standard_dev_all_797$sd_2)
print(summer_sd_mk_797)
## 
##  Mann-Kendall trend test
## 
## data:  summer_standard_dev_all_797$sd_2
## z = -2.8894, n = 31, p-value = 0.00386
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
## -171.0000000 3461.6666667   -0.3677419
summer_sd_sens_797 <- sens.slope(summer_standard_dev_all_797$sd_2)
print(summer_sd_sens_797)
## 
##  Sen's slope
## 
## data:  summer_standard_dev_all_797$sd_2
## z = -2.8894, n = 31, p-value = 0.00386
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.07024220 -0.01064723
## sample estimates:
## Sen's slope 
##  -0.0390493

Winter

winter_standard_dev_all_797 <- standard_dev_797 %>%
  filter(waterDay >= 32 & waterDay <= 182) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())

winter_standard_dev_all_797 <- winter_standard_dev_all_797 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

winter_standard_dev_all_797 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1988 4.803631
1989 5.606557
1990 4.869386
1991 4.723788
1992 8.274717
1995 4.191396
1996 5.047344
1997 4.911177
1998 4.414866
1999 4.076039
2000 4.792439
2001 4.056284
2002 5.051351
2003 6.605544
2004 5.935687
2007 5.158984
2008 5.437225
2009 4.556869
2010 4.265090
2011 5.095026
2012 4.180748
2013 5.560780
2014 4.130668
2015 4.465995
2016 4.828614
2017 5.036457
2018 4.296132
2019 3.977055
2020 4.262699
2021 4.277143
2022 4.644949
ggplot(winter_standard_dev_all_797, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 797 average winter temperatures for water years 2005-2021

winter MK & SS for 797 (non-corrected)

winter_sd_mk_797 <- mk.test(winter_standard_dev_all_797$sd_2)
print(winter_sd_mk_797)
## 
##  Mann-Kendall trend test
## 
## data:  winter_standard_dev_all_797$sd_2
## z = -1.6317, n = 31, p-value = 0.1028
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
##  -97.0000000 3461.6666667   -0.2086022
winter_sd_sens_797 <- sens.slope(winter_standard_dev_all_797$sd_2)
print(winter_sd_sens_797)
## 
##  Sen's slope
## 
## data:  winter_standard_dev_all_797$sd_2
## z = -1.6317, n = 31, p-value = 0.1028
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.047479072  0.004202205
## sample estimates:
## Sen's slope 
## -0.02251882

Summer and Winter SD CORRECTED

Summer

summer_standard_dev_all_797_ad <- standard_dev_797_ad %>% 
  filter(waterDay >= 244 & waterDay <= 335) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())
summer_standard_dev_all_797_ad <- summer_standard_dev_all_797_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
summer_standard_dev_all_797_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1988 2.245034
1989 5.623882
1990 2.834998
1991 4.898820
1992 2.446780
1995 3.243187
1996 2.033137
1997 2.276214
1998 2.950593
1999 2.446501
2000 1.651046
2001 2.140749
2002 3.621940
2003 2.691961
2004 4.139630
2007 2.509660
2008 2.392731
2009 3.033066
2010 2.406356
2011 1.963820
2012 1.650528
2013 1.822647
2014 1.971575
2015 2.431067
2016 2.572877
2017 2.033915
2018 1.929470
2019 2.568532
2020 2.416814
2021 2.211819
2022 2.241472
ggplot(summer_standard_dev_all_797_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 797 average summer temperatures for water years 1986-2021

summer MK & SS 797 (corrected)

summer_sd_mk_797_ad <- mk.test(summer_standard_dev_all_797_ad$sd_2)
print(summer_sd_mk_797_ad)
## 
##  Mann-Kendall trend test
## 
## data:  summer_standard_dev_all_797_ad$sd_2
## z = -2.1076, n = 31, p-value = 0.03507
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
## -125.0000000 3461.6666667   -0.2688172
summer_sd_sens_797_ad <- sens.slope(summer_standard_dev_all_797_ad$sd_2)
print(summer_sd_sens_797_ad)
## 
##  Sen's slope
## 
## data:  summer_standard_dev_all_797_ad$sd_2
## z = -2.1076, n = 31, p-value = 0.03507
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.056855522 -0.001102443
## sample estimates:
## Sen's slope 
## -0.02502598

Winter

winter_standard_dev_all_797_ad <- standard_dev_797_ad %>% 
  filter(waterDay >= 32 & waterDay <= 182) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())
winter_standard_dev_all_797_ad <- winter_standard_dev_all_797_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
winter_standard_dev_all_797_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1988 4.675154
1989 5.455092
1990 4.699934
1991 4.621363
1992 7.618416
1995 4.055758
1996 4.882491
1997 4.733803
1998 4.270832
1999 3.936098
2000 4.596529
2001 3.946228
2002 4.876426
2003 6.186776
2004 5.656983
2007 5.158963
2008 5.435754
2009 4.556054
2010 4.265451
2011 5.094722
2012 4.180574
2013 5.559375
2014 4.130789
2015 4.465776
2016 4.828268
2017 5.035310
2018 4.295282
2019 3.976437
2020 4.262273
2021 4.276695
2022 4.643422
ggplot(winter_standard_dev_all_797_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 797 average winter temperatures for water years 1986-2021

winter MK & SS 797 (corrected)

winter_sd_mk_797_ad <- mk.test(winter_standard_dev_all_797_ad$sd_2)
print(winter_sd_mk_797_ad)
## 
##  Mann-Kendall trend test
## 
## data:  winter_standard_dev_all_797_ad$sd_2
## z = -1.2237, n = 31, p-value = 0.221
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
##  -73.0000000 3461.6666667   -0.1569892
winter_sd_sens_797_ad <- sens.slope(winter_standard_dev_all_797_ad$sd_2)
print(winter_sd_sens_797_ad)
## 
##  Sen's slope
## 
## data:  winter_standard_dev_all_797_ad$sd_2
## z = -1.2237, n = 31, p-value = 0.221
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.040200980  0.009852644
## sample estimates:
## Sen's slope 
## -0.01504631

Upper Rio Grande 839

Oyler -> Morrisey 5/26/2004

snotel_839 <- SNOTEL_san_juan_Area %>% 
  filter(site_id == "839")
#str(snotel_839) # check the date, usually a character.  

snotel_839$Date <- as.Date(snotel_839$date) #change date from character to date format, capitalize to work with Water year functon from NWIS.

#THIS WILL CHANGE FOR EACH STATION
snotel_839_clean <- snotel_839 %>% # filter for the timeframe
  filter(Date >= "1979-10-01" & Date <= "2022-09-30") %>%
  #filter(temperature_mean >= -30 & temperature_mean <= 20) %>% # removing outliers   
  addWaterYear() %>% 
  mutate(daymonth = format(as.Date(Date), "%d-%m")) %>% 
  na.omit()

#adding water day using difftime (SUPER COOL. example from [this](https://stackoverflow.com/questions/48123049/create-day-index-based-on-water-year))

snotel_839_clean <- snotel_839_clean %>% 
  group_by(waterYear)%>% 
  mutate(waterDay = (as.integer(difftime(Date, ymd(paste0(waterYear - 1 ,'-09-30')), units = "days"))))
# Check for outliers

ggplot(snotel_839_clean, aes(x = Date, y = temperature_mean)) +
  geom_point() + #lwd = 2) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature (°C)') + 
  xlab('Date')

snotel_839_clean <- snotel_839_clean %>% 
  mutate(temp_diff = abs(temperature_min - temperature_max)) %>% 
  filter(temperature_mean > -40) %>% 
  filter(temperature_mean < 25)
ggplot(snotel_839_clean, aes(x = Date, y = temp_diff)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  #geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature varience (°C)') + 
  xlab('Date')

Per Steven’s advice: :If there are more than 15 missing days, then…remove that year”

# filtering for temperature anomalies
#snotel_839_cull_count <- snotel_839_clean %>% 
#  filter(temperature_min > -40) %>% 
#  count(waterYear)

#snotel_839_cull_count

# filtering for too few observations in a year
snotel_839_cull_count_days <- snotel_839_clean %>% 
  group_by(waterYear) %>% 
  count(waterYear) %>% 
  filter(n < 350)

snotel_839_cull_count_days
## # A tibble: 5 x 2
## # Groups:   waterYear [5]
##   waterYear     n
##       <dbl> <int>
## 1      1994   338
## 2      2004   330
## 3      2012   339
## 4      2014   313
## 5      2016   347

1994, 2004, 2012, 2014, 2016 need to be culled.

snotel_839_clean_culled <- snotel_839_clean %>% 
  filter(waterYear != "1994" & waterYear != "2004" & waterYear != "2012" & waterYear != "2014" & waterYear != "2016")# & waterYear != "2021" & waterYear != "2022") & waterYear != "2014") #%>% 
  #filter(temperature_mean > -49)

ggplot(snotel_839_clean_culled, aes(x = Date, y = temp_diff)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  #geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature varience (°C)') + 
  xlab('Date')

ggplot(snotel_839_clean_culled, aes(x = Date, y = temperature_mean)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature (°C)') + 
  xlab('Date')

temp_839_xts <- xts(snotel_839_clean_culled$temperature_mean, order.by = snotel_839_clean_culled$Date)

dygraph(temp_839_xts) %>%
  dyAxis("y", label = "Daily mean temperature (°C)") 
#snotel_839_clean_culled <- snotel_839_clean_culled %>% 
#  filter(temperature_mean < 19.3)

temp_839_xts <- xts(snotel_839_clean_culled$temperature_mean, order.by = snotel_839_clean_culled$Date)

dygraph(temp_839_xts) %>%
  dyAxis("y", label = "Daily mean temperature (°C)") 

Upper Rio Grande 839

Oyler -> Morrisey 5/26/2004

snotel_839_adjusted <- snotel_839_clean_culled %>%
  mutate(temp_ad = if_else(Date < "2004-05-26", ((5.3*10^(-7))*(temperature_mean^(4))+(3.72*10^(-5))*(temperature_mean^(3))-(2.16*10^(-3))*(temperature_mean^(2))-(7.32*10^(-2))*(temperature_mean)+1.37)+temperature_mean, temperature_mean))

Non-corrected

839 Detrended

#using the clean culled df:

#average water year temperature

yearly_wy_aver_839 <- snotel_839_adjusted %>% 
  group_by(waterYear) %>% 
  mutate(aver_ann_temp = mean(temperature_mean))

#Average temperature by day for all water years:

daily_wy_aver_839 <- yearly_wy_aver_839 %>% 
  group_by(daymonth) %>% 
  mutate(aver_day_temp = mean(aver_ann_temp))

#average mean temperature by day for the period of record:

daily_wy_aver_839 <- daily_wy_aver_839 %>% 
  group_by(daymonth) %>% 
  mutate(all_ave_temp = mean(daily_wy_aver_839$aver_day_temp))

# try to show all years as means. 
daily_wy_aver2_839 <-daily_wy_aver_839 %>% 
  group_by(waterDay) %>%
  mutate(date_temp = mean(temperature_mean))
  

daily_wy_aver2_839$date_temp <- signif(daily_wy_aver2_839$date_temp,3) #reduce the sig figs


ggplot(daily_wy_aver2_839, aes(x = waterDay, y = date_temp))+
  geom_line(size= 0.7) +
  theme_few() +
  ylab('Average Daily temperature (°C)') + 
  xlab('Day of water year')

839 SD

standard_dev_839 <- daily_wy_aver_839 %>% 
  group_by(waterYear) %>% 
  mutate(residual = (all_ave_temp-aver_ann_temp)+temperature_mean-aver_day_temp) %>% 
  mutate(deviation = abs(residual-lag(residual)))

standard_dev_all_839 <- standard_dev_839 %>% 
  group_by(waterYear) %>% 
  mutate(nmbr = n())

standard_dev_all_839 <- standard_dev_all_839 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

standard_dev_all_839 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 9.000013
1988 9.231875
1989 9.553651
1990 8.539684
1991 9.095321
1992 8.840589
1993 9.034837
1995 8.477482
1996 8.374844
1997 8.635328
1998 9.151866
1999 7.826807
2000 8.411485
2001 9.307564
2002 9.394376
2003 8.653297
2005 8.284320
2006 7.873798
2007 8.375674
2008 8.874800
2009 7.572710
2010 9.043721
2011 8.610385
2013 9.013859
2015 7.623354
2017 7.852672
2018 7.363181
2019 8.357738
2020 8.830386
2021 8.642675
2022 7.975856
ggplot(standard_dev_all_839, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 839 average temperatures for water years 2005-2021

MK & SS for 839 (non-corrected)

sd_mk_839 <- mk.test(standard_dev_all_839$sd_2)
print(sd_mk_839)
## 
##  Mann-Kendall trend test
## 
## data:  standard_dev_all_839$sd_2
## z = -2.5495, n = 31, p-value = 0.01079
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
## -151.0000000 3461.6666667   -0.3247312
sd_sens_839 <- sens.slope(standard_dev_all_839$sd_2)
print(sd_sens_839)
## 
##  Sen's slope
## 
## data:  standard_dev_all_839$sd_2
## z = -2.5495, n = 31, p-value = 0.01079
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.051315272 -0.008246409
## sample estimates:
## Sen's slope 
## -0.03122783

Corrected

839 Detrended (corrected)

#using the clean culled df:
#average water year temperature
yearly_wy_aver_839_ad <- snotel_839_adjusted %>% 
  group_by(waterYear) %>% 
  mutate(aver_ann_temp_ad = mean(temp_ad))
#Average temperature by day for all water years:
daily_wy_aver_839_ad <- yearly_wy_aver_839_ad %>% 
  group_by(daymonth) %>% 
  mutate(aver_day_temp_ad = mean(aver_ann_temp_ad))
#average mean temperature by day for the period of record:
daily_wy_aver_839_ad <- daily_wy_aver_839_ad %>% 
  group_by(daymonth) %>% 
  mutate(all_ave_temp_ad = mean(daily_wy_aver_839_ad$aver_day_temp_ad))
# try to show all years as means. 
daily_wy_aver2_839_ad <-daily_wy_aver_839_ad %>% 
  group_by(waterDay) %>%
  mutate(date_temp_ad = mean(temp_ad))
  
daily_wy_aver2_839_ad$date_temp_ad <- signif(daily_wy_aver2_839_ad$date_temp_ad,3) #reduce the sig figs
ggplot(daily_wy_aver2_839_ad, aes(x = waterDay, y = date_temp_ad))+
  geom_line(size= 0.7) +
  theme_few() +
  ylab('Average Daily temperature (°C)') + 
  xlab('Day of water year')

839 SS (corrected)

standard_dev_839_ad <- daily_wy_aver_839_ad %>% 
  group_by(waterYear) %>% 
  #filter(waterYear >= 1987 & waterYear <= 2021) %>% 
  mutate(residual = (all_ave_temp_ad-aver_ann_temp_ad)+temp_ad-aver_day_temp_ad) %>% 
  mutate(deviation = abs(residual-lag(residual)))
standard_dev_all_839_ad <- standard_dev_839_ad %>% 
  group_by(waterYear) %>% 
  mutate(nmbr = n())
standard_dev_all_839_ad <- standard_dev_all_839_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
standard_dev_all_839_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 8.404486
1988 8.657820
1989 8.999163
1990 7.974877
1991 8.538551
1992 8.275130
1993 8.430766
1995 7.896968
1996 7.767745
1997 8.045426
1998 8.511594
1999 7.265049
2000 7.783278
2001 8.666438
2002 8.721243
2003 8.026530
2005 8.284037
2006 7.873931
2007 8.375662
2008 8.875360
2009 7.573403
2010 9.044246
2011 8.610915
2013 9.013498
2015 7.624141
2017 7.852753
2018 7.363589
2019 8.358178
2020 8.831049
2021 8.643116
2022 7.976653
ggplot(standard_dev_all_839_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 839 average temperatures for water years 1986-2021

MK & SS 839 (corrected)

sd_mk_839_ad <- mk.test(standard_dev_all_839_ad$sd_2)
print(sd_mk_839_ad)
## 
##  Mann-Kendall trend test
## 
## data:  standard_dev_all_839_ad$sd_2
## z = -0.16996, n = 31, p-value = 0.865
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##             S          varS           tau 
##  -11.00000000 3461.66666667   -0.02365591
sd_sens_839_ad <- sens.slope(standard_dev_all_839_ad$sd_2)
print(sd_sens_839_ad)
## 
##  Sen's slope
## 
## data:  standard_dev_all_839_ad$sd_2
## z = -0.16996, n = 31, p-value = 0.865
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.02519708  0.02245395
## sample estimates:
##  Sen's slope 
## -0.002233571

Summer and Winter SD NOT-CORRECTED

Summer

summer_standard_dev_all_839 <- standard_dev_839 %>%
  filter(waterDay >= 244 & waterDay <= 335) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())

summer_standard_dev_all_839 <- summer_standard_dev_all_839 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

summer_standard_dev_all_839 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 2.339645
1988 2.409039
1989 3.000858
1990 2.873313
1991 2.515342
1992 2.864531
1993 2.899106
1995 2.810444
1996 2.024826
1997 2.150453
1998 2.670019
1999 2.391108
2000 1.660533
2001 2.194305
2002 2.195636
2003 5.141657
2005 2.571311
2006 1.780020
2007 2.008229
2008 1.893666
2009 2.494618
2010 2.130592
2011 1.651068
2013 1.798254
2015 2.039987
2017 1.785569
2018 1.687691
2019 2.071325
2020 1.899239
2021 1.936877
2022 1.802188
ggplot(summer_standard_dev_all_839, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 839 average summer temperatures for water years 2005-2021

summer MK & SS for 839 (non-corrected)

summer_sd_mk_839 <- mk.test(summer_standard_dev_all_839$sd_2)
print(summer_sd_mk_839)
## 
##  Mann-Kendall trend test
## 
## data:  summer_standard_dev_all_839$sd_2
## z = -3.6372, n = 31, p-value = 0.0002756
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
## -215.0000000 3461.6666667   -0.4623656
summer_sd_sens_839 <- sens.slope(summer_standard_dev_all_839$sd_2)
print(summer_sd_sens_839)
## 
##  Sen's slope
## 
## data:  summer_standard_dev_all_839$sd_2
## z = -3.6372, n = 31, p-value = 0.0002756
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.04383721 -0.01789452
## sample estimates:
## Sen's slope 
## -0.03129896

Winter

winter_standard_dev_all_839 <- standard_dev_839 %>%
  filter(waterDay >= 32 & waterDay <= 182) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())

winter_standard_dev_all_839 <- winter_standard_dev_all_839 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

winter_standard_dev_all_839 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 4.398805
1988 5.590658
1989 6.853499
1990 5.272310
1991 5.213793
1992 5.050047
1993 4.722721
1995 5.134365
1996 4.847138
1997 5.056218
1998 4.784267
1999 4.600428
2000 4.565157
2001 4.566478
2002 5.212259
2003 4.381878
2005 4.302631
2006 4.639049
2007 5.599134
2008 6.135623
2009 4.558676
2010 4.897715
2011 5.631610
2013 5.742855
2015 5.016585
2017 5.707411
2018 4.346846
2019 4.407444
2020 5.155251
2021 4.616505
2022 4.708981
ggplot(winter_standard_dev_all_839, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 839 average winter temperatures for water years 2005-2021

winter MK & SS for 839 (non-corrected)

winter_sd_mk_839 <- mk.test(winter_standard_dev_all_839$sd_2)
print(winter_sd_mk_839)
## 
##  Mann-Kendall trend test
## 
## data:  winter_standard_dev_all_839$sd_2
## z = -0.91781, n = 31, p-value = 0.3587
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
##  -55.0000000 3461.6666667   -0.1182796
winter_sd_sens_839 <- sens.slope(winter_standard_dev_all_839$sd_2)
print(winter_sd_sens_839)
## 
##  Sen's slope
## 
## data:  winter_standard_dev_all_839$sd_2
## z = -0.91781, n = 31, p-value = 0.3587
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.03527040  0.01166625
## sample estimates:
## Sen's slope 
## -0.01171751

Summer and Winter SD CORRECTED

Summer

summer_standard_dev_all_839_ad <- standard_dev_839_ad %>% 
  filter(waterDay >= 244 & waterDay <= 335) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())
summer_standard_dev_all_839_ad <- summer_standard_dev_all_839_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
summer_standard_dev_all_839_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 2.100721
1988 2.165432
1989 2.694263
1990 2.586843
1991 2.264622
1992 2.577564
1993 2.603045
1995 2.521410
1996 1.812342
1997 1.930230
1998 2.393978
1999 2.149070
2000 1.485761
2001 1.966973
2002 1.964327
2003 4.629268
2005 2.570019
2006 1.778567
2007 2.008120
2008 1.893140
2009 2.494537
2010 2.130945
2011 1.651218
2013 1.797667
2015 2.039573
2017 1.785650
2018 1.687040
2019 2.070803
2020 1.898818
2021 1.936581
2022 1.802555
ggplot(summer_standard_dev_all_839_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 839 average summer temperatures for water years 1986-2021

summer MK & SS 839 (corrected)

summer_sd_mk_839_ad <- mk.test(summer_standard_dev_all_839_ad$sd_2)
print(summer_sd_mk_839_ad)
## 
##  Mann-Kendall trend test
## 
## data:  summer_standard_dev_all_839_ad$sd_2
## z = -2.8894, n = 31, p-value = 0.00386
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
## -171.0000000 3461.6666667   -0.3677419
summer_sd_sens_839_ad <- sens.slope(summer_standard_dev_all_839_ad$sd_2)
print(summer_sd_sens_839_ad)
## 
##  Sen's slope
## 
## data:  summer_standard_dev_all_839_ad$sd_2
## z = -2.8894, n = 31, p-value = 0.00386
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.031254569 -0.005659981
## sample estimates:
## Sen's slope 
## -0.01895022

Winter

winter_standard_dev_all_839_ad <- standard_dev_839_ad %>% 
  filter(waterDay >= 32 & waterDay <= 182) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())
winter_standard_dev_all_839_ad <- winter_standard_dev_all_839_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
winter_standard_dev_all_839_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 4.287106
1988 5.464765
1989 6.712024
1990 5.114028
1991 5.120770
1992 4.913951
1993 4.586285
1995 4.965760
1996 4.676376
1997 4.889709
1998 4.616778
1999 4.426756
2000 4.386454
2001 4.434810
2002 5.014547
2003 4.228870
2005 4.301491
2006 4.640098
2007 5.598865
2008 6.136443
2009 4.560635
2010 4.898822
2011 5.633033
2013 5.744435
2015 5.018158
2017 5.707307
2018 4.347866
2019 4.408918
2020 5.156412
2021 4.617938
2022 4.711537
ggplot(winter_standard_dev_all_839_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 839 average winter temperatures for water years 1986-2021

winter MK & SS 839 (corrected)

winter_sd_mk_839_ad <- mk.test(winter_standard_dev_all_839_ad$sd_2)
print(winter_sd_mk_839_ad)
## 
##  Mann-Kendall trend test
## 
## data:  winter_standard_dev_all_839_ad$sd_2
## z = -0.10198, n = 31, p-value = 0.9188
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##             S          varS           tau 
##   -7.00000000 3461.66666667   -0.01505376
winter_sd_sens_839_ad <- sens.slope(winter_standard_dev_all_839_ad$sd_2)
print(winter_sd_sens_839_ad)
## 
##  Sen's slope
## 
## data:  winter_standard_dev_all_839_ad$sd_2
## z = -0.10198, n = 31, p-value = 0.9188
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.02991581  0.01806017
## sample estimates:
## Sen's slope 
## -0.00184666

Upper San Juan 840

Morrisey 4/7/2004

snotel_840 <- SNOTEL_san_juan_Area %>% 
  filter(site_id == "840")
#str(snotel_840) # check the date, usually a character.  

snotel_840$Date <- as.Date(snotel_840$date) #change date from character to date format, capitalize to work with Water year functon from NWIS.

#THIS WILL CHANGE FOR EACH STATION
snotel_840_clean <- snotel_840 %>% # filter for the timeframe
  filter(Date >= "1979-10-01" & Date <= "2022-09-30") %>%
  #filter(temperature_mean >= -30 & temperature_mean <= 20) %>% # removing outliers   
  addWaterYear() %>% 
  mutate(daymonth = format(as.Date(Date), "%d-%m")) %>% 
  na.omit()

#adding water day using difftime (SUPER COOL. example from [this](https://stackoverflow.com/questions/48123049/create-day-index-based-on-water-year))

snotel_840_clean <- snotel_840_clean %>% 
  group_by(waterYear)%>% 
  mutate(waterDay = (as.integer(difftime(Date, ymd(paste0(waterYear - 1 ,'-09-30')), units = "days"))))
# Check for outliers

ggplot(snotel_840_clean, aes(x = Date, y = temperature_mean)) +
  geom_point() + #lwd = 2) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature (°C)') + 
  xlab('Date')

snotel_840_clean <- snotel_840_clean %>% 
  mutate(temp_diff = abs(temperature_min - temperature_max)) %>% 
  filter(temperature_mean > -40) %>% 
  filter(temperature_mean < 25)
ggplot(snotel_840_clean, aes(x = Date, y = temp_diff)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  #geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature varience (°C)') + 
  xlab('Date')

Per Steven’s advice: :If there are more than 15 missing days, then…remove that year”

# filtering for temperature anomalies
#snotel_840_cull_count <- snotel_840_clean %>% 
#  filter(temperature_min > -40) %>% 
#  count(waterYear)

#snotel_840_cull_count

# filtering for too few observations in a year
snotel_840_cull_count_days <- snotel_840_clean %>% 
  group_by(waterYear) %>% 
  count(waterYear) %>% 
  filter(n < 350)

snotel_840_cull_count_days
## # A tibble: 11 x 2
## # Groups:   waterYear [11]
##    waterYear     n
##        <dbl> <int>
##  1      1980   310
##  2      1981   274
##  3      1982     1
##  4      1983   318
##  5      1984   322
##  6      1985   275
##  7      1993   349
##  8      1994   345
##  9      2005   309
## 10      2006   325
## 11      2013   314

1980, 1981, 1982, 1983, 1984, 1985, 1993, 1994, 2005, 2006, 2013 need to be culled.

snotel_840_clean_culled <- snotel_840_clean %>% 
  filter(waterYear >= "1986" & waterYear != "1993" & waterYear != "1994" & waterYear != "2005" & waterYear != "2006" & waterYear != "2013")# & waterYear != "2022") & waterYear != "2014") #%>% 
  #filter(temperature_mean > -49)

ggplot(snotel_840_clean_culled, aes(x = Date, y = temp_diff)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  #geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature varience (°C)') + 
  xlab('Date')

ggplot(snotel_840_clean_culled, aes(x = Date, y = temperature_mean)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature (°C)') + 
  xlab('Date')

temp_840_xts <- xts(snotel_840_clean_culled$temperature_mean, order.by = snotel_840_clean_culled$Date)

dygraph(temp_840_xts) %>%
  dyAxis("y", label = "Daily mean temperature (°C)") 
#snotel_840_clean_culled <- snotel_840_clean_culled %>% 
#  filter(temperature_mean < 19.3)

temp_840_xts <- xts(snotel_840_clean_culled$temperature_mean, order.by = snotel_840_clean_culled$Date)

dygraph(temp_840_xts) %>%
  dyAxis("y", label = "Daily mean temperature (°C)") 

Upper San Juan 840

Morrisey 4/7/2004

snotel_840_adjusted <- snotel_840_clean_culled %>%
  mutate(temp_ad = if_else(Date < "2004-04-07", ((5.3*10^(-7))*(temperature_mean^(4))+(3.72*10^(-5))*(temperature_mean^(3))-(2.16*10^(-3))*(temperature_mean^(2))-(7.32*10^(-2))*(temperature_mean)+1.37)+temperature_mean, temperature_mean))

Non-corrected

840 Detrended

#using the clean culled df:

#average water year temperature

yearly_wy_aver_840 <- snotel_840_adjusted %>% 
  group_by(waterYear) %>% 
  mutate(aver_ann_temp = mean(temperature_mean))

#Average temperature by day for all water years:

daily_wy_aver_840 <- yearly_wy_aver_840 %>% 
  group_by(daymonth) %>% 
  mutate(aver_day_temp = mean(aver_ann_temp))

#average mean temperature by day for the period of record:

daily_wy_aver_840 <- daily_wy_aver_840 %>% 
  group_by(daymonth) %>% 
  mutate(all_ave_temp = mean(daily_wy_aver_840$aver_day_temp))

# try to show all years as means. 
daily_wy_aver2_840 <-daily_wy_aver_840 %>% 
  group_by(waterDay) %>%
  mutate(date_temp = mean(temperature_mean))
  

daily_wy_aver2_840$date_temp <- signif(daily_wy_aver2_840$date_temp,3) #reduce the sig figs


ggplot(daily_wy_aver2_840, aes(x = waterDay, y = date_temp))+
  geom_line(size= 0.7) +
  theme_few() +
  ylab('Average Daily temperature (°C)') + 
  xlab('Day of water year')

840 SD

standard_dev_840 <- daily_wy_aver_840 %>% 
  group_by(waterYear) %>% 
  mutate(residual = (all_ave_temp-aver_ann_temp)+temperature_mean-aver_day_temp) %>% 
  mutate(deviation = abs(residual-lag(residual)))

standard_dev_all_840 <- standard_dev_840 %>% 
  group_by(waterYear) %>% 
  mutate(nmbr = n())

standard_dev_all_840 <- standard_dev_all_840 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

standard_dev_all_840 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1986 7.232619
1987 7.739156
1988 8.527104
1989 8.409877
1990 7.857130
1991 8.153466
1992 7.489499
1995 7.461187
1996 7.863786
1997 7.870552
1998 8.397044
1999 7.262936
2000 7.859027
2001 8.336002
2002 8.619541
2003 8.032941
2004 8.221773
2007 7.731123
2008 7.978427
2009 7.095790
2010 8.100698
2011 7.956902
2012 7.805592
2014 7.587053
2015 6.894972
2016 7.701017
2017 7.249233
2018 7.180902
2019 7.751539
2020 7.888215
2021 7.886812
2022 7.530559
ggplot(standard_dev_all_840, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 840 average temperatures for water years 2005-2021

MK & SS for 840 (non-corrected)

sd_mk_840 <- mk.test(standard_dev_all_840$sd_2)
print(sd_mk_840)
## 
##  Mann-Kendall trend test
## 
## data:  standard_dev_all_840$sd_2
## z = -1.5406, n = 32, p-value = 0.1234
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
##  -96.0000000 3802.6666667   -0.1935484
sd_sens_840 <- sens.slope(standard_dev_all_840$sd_2)
print(sd_sens_840)
## 
##  Sen's slope
## 
## data:  standard_dev_all_840$sd_2
## z = -1.5406, n = 32, p-value = 0.1234
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.031404215  0.001716915
## sample estimates:
## Sen's slope 
##  -0.0141416

Corrected

840 Detrended (corrected)

#using the clean culled df:
#average water year temperature
yearly_wy_aver_840_ad <- snotel_840_adjusted %>% 
  group_by(waterYear) %>% 
  mutate(aver_ann_temp_ad = mean(temp_ad))
#Average temperature by day for all water years:
daily_wy_aver_840_ad <- yearly_wy_aver_840_ad %>% 
  group_by(daymonth) %>% 
  mutate(aver_day_temp_ad = mean(aver_ann_temp_ad))
#average mean temperature by day for the period of record:
daily_wy_aver_840_ad <- daily_wy_aver_840_ad %>% 
  group_by(daymonth) %>% 
  mutate(all_ave_temp_ad = mean(daily_wy_aver_840_ad$aver_day_temp_ad))
# try to show all years as means. 
daily_wy_aver2_840_ad <-daily_wy_aver_840_ad %>% 
  group_by(waterDay) %>%
  mutate(date_temp_ad = mean(temp_ad))
  
daily_wy_aver2_840_ad$date_temp_ad <- signif(daily_wy_aver2_840_ad$date_temp_ad,3) #reduce the sig figs
ggplot(daily_wy_aver2_840_ad, aes(x = waterDay, y = date_temp_ad))+
  geom_line(size= 0.7) +
  theme_few() +
  ylab('Average Daily temperature (°C)') + 
  xlab('Day of water year')

840 SS (corrected)

standard_dev_840_ad <- daily_wy_aver_840_ad %>% 
  group_by(waterYear) %>% 
  #filter(waterYear >= 1987 & waterYear <= 2021) %>% 
  mutate(residual = (all_ave_temp_ad-aver_ann_temp_ad)+temp_ad-aver_day_temp_ad) %>% 
  mutate(deviation = abs(residual-lag(residual)))
standard_dev_all_840_ad <- standard_dev_840_ad %>% 
  group_by(waterYear) %>% 
  mutate(nmbr = n())
standard_dev_all_840_ad <- standard_dev_all_840_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
standard_dev_all_840_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1986 6.729334
1987 7.211134
1988 7.975188
1989 7.874323
1990 7.334504
1991 7.648553
1992 6.993487
1995 6.945265
1996 7.326356
1997 7.346542
1998 7.819710
1999 6.756828
2000 7.298996
2001 7.766886
2002 8.021992
2003 7.460019
2004 7.506468
2007 7.729865
2008 7.977294
2009 7.094470
2010 8.099982
2011 7.955897
2012 7.804828
2014 7.585948
2015 6.894260
2016 7.700243
2017 7.247788
2018 7.180489
2019 7.751133
2020 7.887037
2021 7.885810
2022 7.530352
ggplot(standard_dev_all_840_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 840 average temperatures for water years 1986-2021

MK & SS 840 (corrected)

sd_mk_840_ad <- mk.test(standard_dev_all_840_ad$sd_2)
print(sd_mk_840_ad)
## 
##  Mann-Kendall trend test
## 
## data:  standard_dev_all_840_ad$sd_2
## z = 1.2811, n = 32, p-value = 0.2002
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
##   80.0000000 3802.6666667    0.1612903
sd_sens_840_ad <- sens.slope(standard_dev_all_840_ad$sd_2)
print(sd_sens_840_ad)
## 
##  Sen's slope
## 
## data:  standard_dev_all_840_ad$sd_2
## z = 1.2811, n = 32, p-value = 0.2002
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.005553565  0.027024754
## sample estimates:
## Sen's slope 
##   0.0120862

Summer and Winter SD NOT-CORRECTED

Summer

summer_standard_dev_all_840 <- standard_dev_840 %>%
  filter(waterDay >= 244 & waterDay <= 335) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())

summer_standard_dev_all_840 <- summer_standard_dev_all_840 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

summer_standard_dev_all_840 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1986 2.274917
1987 2.014396
1988 2.306036
1989 2.601399
1990 2.247671
1991 2.398805
1992 2.430312
1995 3.100283
1996 1.667998
1997 2.117495
1998 2.789071
1999 2.553329
2000 1.581360
2001 2.145173
2002 1.886740
2003 3.709504
2004 1.898981
2007 2.169470
2008 2.201448
2009 2.474110
2010 1.975030
2011 1.916261
2012 1.403117
2014 1.738023
2015 1.863685
2016 2.211199
2017 1.720494
2018 1.737876
2019 2.178636
2020 1.812582
2021 1.913112
2022 1.866278
ggplot(summer_standard_dev_all_840, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 840 average summer temperatures for water years 2005-2021

summer MK & SS for 840 (non-corrected)

summer_sd_mk_840 <- mk.test(summer_standard_dev_all_840$sd_2)
print(summer_sd_mk_840)
## 
##  Mann-Kendall trend test
## 
## data:  summer_standard_dev_all_840$sd_2
## z = -2.546, n = 32, p-value = 0.0109
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
## -158.0000000 3802.6666667   -0.3185484
summer_sd_sens_840 <- sens.slope(summer_standard_dev_all_840$sd_2)
print(summer_sd_sens_840)
## 
##  Sen's slope
## 
## data:  summer_standard_dev_all_840$sd_2
## z = -2.546, n = 32, p-value = 0.0109
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.030042231 -0.004937269
## sample estimates:
## Sen's slope 
## -0.01778031

Winter

winter_standard_dev_all_840 <- standard_dev_840 %>%
  filter(waterDay >= 32 & waterDay <= 182) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())

winter_standard_dev_all_840 <- winter_standard_dev_all_840 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

winter_standard_dev_all_840 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1986 4.774693
1987 3.822681
1988 4.648221
1989 5.389172
1990 4.690399
1991 4.527467
1992 3.655616
1995 4.088895
1996 4.445300
1997 4.471830
1998 4.047841
1999 4.028955
2000 4.447934
2001 3.687388
2002 4.814735
2003 3.749425
2004 5.060374
2007 4.823105
2008 5.320894
2009 4.166805
2010 4.213437
2011 4.840786
2012 3.843512
2014 3.850854
2015 4.398650
2016 4.360824
2017 4.818284
2018 3.997183
2019 3.686866
2020 4.015668
2021 4.025477
2022 4.164016
ggplot(winter_standard_dev_all_840, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 840 average winter temperatures for water years 2005-2021

winter MK & SS for 840 (non-corrected)

winter_sd_mk_840 <- mk.test(winter_standard_dev_all_840$sd_2)
print(winter_sd_mk_840)
## 
##  Mann-Kendall trend test
## 
## data:  winter_standard_dev_all_840$sd_2
## z = -1.1838, n = 32, p-value = 0.2365
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
##  -74.0000000 3802.6666667   -0.1491935
winter_sd_sens_840 <- sens.slope(winter_standard_dev_all_840$sd_2)
print(winter_sd_sens_840)
## 
##  Sen's slope
## 
## data:  winter_standard_dev_all_840$sd_2
## z = -1.1838, n = 32, p-value = 0.2365
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.029810141  0.008846282
## sample estimates:
## Sen's slope 
##  -0.0142895

Summer and Winter SD CORRECTED

Summer

summer_standard_dev_all_840_ad <- standard_dev_840_ad %>% 
  filter(waterDay >= 244 & waterDay <= 335) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())
summer_standard_dev_all_840_ad <- summer_standard_dev_all_840_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
summer_standard_dev_all_840_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1986 2.048138
1987 1.805643
1988 2.080518
1989 2.332076
1990 2.018498
1991 2.161691
1992 2.189199
1995 2.788317
1996 1.495646
1997 1.905083
1998 2.507667
1999 2.300968
2000 1.413289
2001 1.928350
2002 1.687157
2003 3.345166
2004 1.896649
2007 2.165544
2008 2.195558
2009 2.468649
2010 1.973357
2011 1.910791
2012 1.400205
2014 1.732726
2015 1.861873
2016 2.207700
2017 1.714988
2018 1.734873
2019 2.175222
2020 1.808042
2021 1.908372
2022 1.866056
ggplot(summer_standard_dev_all_840_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 840 average summer temperatures for water years 1986-2021

summer MK & SS 840 (corrected)

summer_sd_mk_840_ad <- mk.test(summer_standard_dev_all_840_ad$sd_2)
print(summer_sd_mk_840_ad)
## 
##  Mann-Kendall trend test
## 
## data:  summer_standard_dev_all_840_ad$sd_2
## z = -1.3135, n = 32, p-value = 0.189
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
##  -82.0000000 3802.6666667   -0.1653226
summer_sd_sens_840_ad <- sens.slope(summer_standard_dev_all_840_ad$sd_2)
print(summer_sd_sens_840_ad)
## 
##  Sen's slope
## 
## data:  summer_standard_dev_all_840_ad$sd_2
## z = -1.3135, n = 32, p-value = 0.189
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.019782521  0.003642474
## sample estimates:
##  Sen's slope 
## -0.007464632

Winter

winter_standard_dev_all_840_ad <- standard_dev_840_ad %>% 
  filter(waterDay >= 32 & waterDay <= 182) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())
winter_standard_dev_all_840_ad <- winter_standard_dev_all_840_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
winter_standard_dev_all_840_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1986 4.554830
1987 3.694189
1988 4.518723
1989 5.236614
1990 4.529536
1991 4.434653
1992 3.543302
1995 3.950999
1996 4.306141
1997 4.322590
1998 3.908977
1999 3.880739
2000 4.275877
2001 3.576831
2002 4.625719
2003 3.625912
2004 4.900833
2007 4.822356
2008 5.320773
2009 4.166644
2010 4.213133
2011 4.841004
2012 3.843578
2014 3.851521
2015 4.398320
2016 4.361393
2017 4.817629
2018 3.996842
2019 3.687656
2020 4.015777
2021 4.025580
2022 4.164509
ggplot(winter_standard_dev_all_840_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 840 average winter temperatures for water years 1986-2021

winter MK & SS 840 (corrected)

winter_sd_mk_840_ad <- mk.test(winter_standard_dev_all_840_ad$sd_2)
print(winter_sd_mk_840_ad)
## 
##  Mann-Kendall trend test
## 
## data:  winter_standard_dev_all_840_ad$sd_2
## z = -0.63244, n = 32, p-value = 0.5271
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##             S          varS           tau 
##  -40.00000000 3802.66666667   -0.08064516
winter_sd_sens_840_ad <- sens.slope(winter_standard_dev_all_840_ad$sd_2)
print(winter_sd_sens_840_ad)
## 
##  Sen's slope
## 
## data:  winter_standard_dev_all_840_ad$sd_2
## z = -0.63244, n = 32, p-value = 0.5271
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.02273985  0.01362733
## sample estimates:
##  Sen's slope 
## -0.007000917

Vallecito 843

Morrisey 7/22/2005

snotel_843 <- SNOTEL_san_juan_Area %>% 
  filter(site_id == "843")
#str(snotel_843) # check the date, usually a character.  

snotel_843$Date <- as.Date(snotel_843$date) #change date from character to date format, capitalize to work with Water year functon from NWIS.

#THIS WILL CHANGE FOR EACH STATION
snotel_843_clean <- snotel_843 %>% # filter for the timeframe
  filter(Date >= "1979-10-01" & Date <= "2022-09-30") %>%
  #filter(temperature_mean >= -30 & temperature_mean <= 20) %>% # removing outliers   
  addWaterYear() %>% 
  mutate(daymonth = format(as.Date(Date), "%d-%m")) %>% 
  na.omit()

#adding water day using difftime (SUPER COOL. example from [this](https://stackoverflow.com/questions/48123049/create-day-index-based-on-water-year))

snotel_843_clean <- snotel_843_clean %>% 
  group_by(waterYear)%>% 
  mutate(waterDay = (as.integer(difftime(Date, ymd(paste0(waterYear - 1 ,'-09-30')), units = "days"))))
# Check for outliers

ggplot(snotel_843_clean, aes(x = Date, y = temperature_mean)) +
  geom_point() + #lwd = 2) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature (°C)') + 
  xlab('Date')

snotel_843_clean <- snotel_843_clean %>% 
  mutate(temp_diff = abs(temperature_min - temperature_max)) %>% 
  filter(temperature_mean > -40) %>% 
  filter(temperature_mean < 25)
ggplot(snotel_843_clean, aes(x = Date, y = temp_diff)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  #geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature varience (°C)') + 
  xlab('Date')

Per Steven’s advice: :If there are more than 15 missing days, then…remove that year”

# filtering for temperature anomalies
#snotel_843_cull_count <- snotel_843_clean %>% 
#  filter(temperature_min > -40) %>% 
#  count(waterYear)

#snotel_843_cull_count

# filtering for too few observations in a year
snotel_843_cull_count_days <- snotel_843_clean %>% 
  group_by(waterYear) %>% 
  count(waterYear) %>% 
  filter(n < 350)

snotel_843_cull_count_days
## # A tibble: 2 x 2
## # Groups:   waterYear [2]
##   waterYear     n
##       <dbl> <int>
## 1      1994   339
## 2      2003   313

1994 & 2003 need to be culled.

snotel_843_clean_culled <- snotel_843_clean %>% 
  filter(waterYear != "1994" & waterYear != "2003")# & waterYear != "1994" & waterYear != "2005" & waterYear != "2006" & waterYear != "2013")# & waterYear != "2022") & waterYear != "2014") #%>% 
  #filter(temperature_mean > -49)

ggplot(snotel_843_clean_culled, aes(x = Date, y = temp_diff)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  #geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature varience (°C)') + 
  xlab('Date')

ggplot(snotel_843_clean_culled, aes(x = Date, y = temperature_mean)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature (°C)') + 
  xlab('Date')

temp_843_xts <- xts(snotel_843_clean_culled$temperature_mean, order.by = snotel_843_clean_culled$Date)

dygraph(temp_843_xts) %>%
  dyAxis("y", label = "Daily mean temperature (°C)") 
#snotel_843_clean_culled <- snotel_843_clean_culled %>% 
#  filter(temperature_mean < 19.3)

temp_843_xts <- xts(snotel_843_clean_culled$temperature_mean, order.by = snotel_843_clean_culled$Date)

dygraph(temp_843_xts) %>%
  dyAxis("y", label = "Daily mean temperature (°C)") 

Vallecito 843

Morrisey 7/22/2005

snotel_843_adjusted <- snotel_843_clean_culled %>%
  mutate(temp_ad = if_else(Date < "2005-07-22", ((5.3*10^(-7))*(temperature_mean^(4))+(3.72*10^(-5))*(temperature_mean^(3))-(2.16*10^(-3))*(temperature_mean^(2))-(7.32*10^(-2))*(temperature_mean)+1.37)+temperature_mean, temperature_mean))

Non-corrected

843 Detrended

#using the clean culled df:

#average water year temperature

yearly_wy_aver_843 <- snotel_843_adjusted %>% 
  group_by(waterYear) %>% 
  mutate(aver_ann_temp = mean(temperature_mean))

#Average temperature by day for all water years:

daily_wy_aver_843 <- yearly_wy_aver_843 %>% 
  group_by(daymonth) %>% 
  mutate(aver_day_temp = mean(aver_ann_temp))

#average mean temperature by day for the period of record:

daily_wy_aver_843 <- daily_wy_aver_843 %>% 
  group_by(daymonth) %>% 
  mutate(all_ave_temp = mean(daily_wy_aver_843$aver_day_temp))

# try to show all years as means. 
daily_wy_aver2_843 <-daily_wy_aver_843 %>% 
  group_by(waterDay) %>%
  mutate(date_temp = mean(temperature_mean))
  

daily_wy_aver2_843$date_temp <- signif(daily_wy_aver2_843$date_temp,3) #reduce the sig figs


ggplot(daily_wy_aver2_843, aes(x = waterDay, y = date_temp))+
  geom_line(size= 0.7) +
  theme_few() +
  ylab('Average Daily temperature (°C)') + 
  xlab('Day of water year')

843 SD

standard_dev_843 <- daily_wy_aver_843 %>% 
  group_by(waterYear) %>% 
  mutate(residual = (all_ave_temp-aver_ann_temp)+temperature_mean-aver_day_temp) %>% 
  mutate(deviation = abs(residual-lag(residual)))

standard_dev_all_843 <- standard_dev_843 %>% 
  group_by(waterYear) %>% 
  mutate(nmbr = n())

standard_dev_all_843 <- standard_dev_all_843 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

standard_dev_all_843 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 7.744307
1988 8.133975
1989 8.353802
1990 7.814664
1991 7.913458
1992 7.258470
1993 8.017543
1995 7.737939
1996 8.012757
1997 8.122829
1998 8.284299
1999 6.874497
2000 8.036954
2001 8.326535
2002 8.475624
2004 5.169906
2005 7.879202
2006 7.087987
2007 7.572592
2008 8.035842
2009 7.216959
2010 7.825626
2011 7.886538
2012 7.648815
2013 8.040098
2014 7.292347
2015 6.734206
2016 7.523001
2017 7.419976
2018 7.106676
2019 7.826098
2020 7.899858
2021 7.970432
2022 7.307690
ggplot(standard_dev_all_843, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 843 average temperatures for water years 2005-2021

MK & SS for 843 (non-corrected)

sd_mk_843 <- mk.test(standard_dev_all_843$sd_2)
print(sd_mk_843)
## 
##  Mann-Kendall trend test
## 
## data:  standard_dev_all_843$sd_2
## z = -1.6307, n = 34, p-value = 0.103
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##           S        varS         tau 
## -111.000000 4550.333333   -0.197861
sd_sens_843 <- sens.slope(standard_dev_all_843$sd_2)
print(sd_sens_843)
## 
##  Sen's slope
## 
## data:  standard_dev_all_843$sd_2
## z = -1.6307, n = 34, p-value = 0.103
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.029639051  0.001693822
## sample estimates:
## Sen's slope 
## -0.01383409

Corrected

843 Detrended (corrected)

#using the clean culled df:
#average water year temperature
yearly_wy_aver_843_ad <- snotel_843_adjusted %>% 
  group_by(waterYear) %>% 
  mutate(aver_ann_temp_ad = mean(temp_ad))
#Average temperature by day for all water years:
daily_wy_aver_843_ad <- yearly_wy_aver_843_ad %>% 
  group_by(daymonth) %>% 
  mutate(aver_day_temp_ad = mean(aver_ann_temp_ad))
#average mean temperature by day for the period of record:
daily_wy_aver_843_ad <- daily_wy_aver_843_ad %>% 
  group_by(daymonth) %>% 
  mutate(all_ave_temp_ad = mean(daily_wy_aver_843_ad$aver_day_temp_ad))
# try to show all years as means. 
daily_wy_aver2_843_ad <-daily_wy_aver_843_ad %>% 
  group_by(waterDay) %>%
  mutate(date_temp_ad = mean(temp_ad))
  
daily_wy_aver2_843_ad$date_temp_ad <- signif(daily_wy_aver2_843_ad$date_temp_ad,3) #reduce the sig figs
ggplot(daily_wy_aver2_843_ad, aes(x = waterDay, y = date_temp_ad))+
  geom_line(size= 0.7) +
  theme_few() +
  ylab('Average Daily temperature (°C)') + 
  xlab('Day of water year')

843 SS (corrected)

standard_dev_843_ad <- daily_wy_aver_843_ad %>% 
  group_by(waterYear) %>% 
  #filter(waterYear >= 1987 & waterYear <= 2021) %>% 
  mutate(residual = (all_ave_temp_ad-aver_ann_temp_ad)+temp_ad-aver_day_temp_ad) %>% 
  mutate(deviation = abs(residual-lag(residual)))
standard_dev_all_843_ad <- standard_dev_843_ad %>% 
  group_by(waterYear) %>% 
  mutate(nmbr = n())
standard_dev_all_843_ad <- standard_dev_all_843_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
standard_dev_all_843_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 7.167709
1988 7.554905
1989 7.775142
1990 7.236968
1991 7.363376
1992 6.713681
1993 7.440691
1995 7.144630
1996 7.409097
1997 7.529924
1998 7.661187
1999 6.355448
2000 7.405969
2001 7.695693
2002 7.828232
2004 4.700776
2005 7.205286
2006 7.087285
2007 7.571307
2008 8.034938
2009 7.215649
2010 7.824301
2011 7.885342
2012 7.647841
2013 8.038906
2014 7.290741
2015 6.733530
2016 7.521405
2017 7.418681
2018 7.106001
2019 7.824924
2020 7.898803
2021 7.969378
2022 7.306728
ggplot(standard_dev_all_843_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 843 average temperatures for water years 1986-2021

MK & SS 843 (corrected)

sd_mk_843_ad <- mk.test(standard_dev_all_843_ad$sd_2)
print(sd_mk_843_ad)
## 
##  Mann-Kendall trend test
## 
## data:  standard_dev_all_843_ad$sd_2
## z = 1.601, n = 34, p-value = 0.1094
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
##  109.0000000 4550.3333333    0.1942959
sd_sens_843_ad <- sens.slope(standard_dev_all_843_ad$sd_2)
print(sd_sens_843_ad)
## 
##  Sen's slope
## 
## data:  standard_dev_all_843_ad$sd_2
## z = 1.601, n = 34, p-value = 0.1094
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.003122605  0.025938612
## sample estimates:
## Sen's slope 
##  0.01173229

Summer and Winter SD NOT-CORRECTED

Summer

summer_standard_dev_all_843 <- standard_dev_843 %>%
  filter(waterDay >= 244 & waterDay <= 335) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())

summer_standard_dev_all_843 <- summer_standard_dev_all_843 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

summer_standard_dev_all_843 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 2.346128
1988 2.619944
1989 3.207520
1990 3.072873
1991 2.786863
1992 3.068047
1993 3.467390
1995 3.677577
1996 2.664453
1997 2.702126
1998 3.450109
1999 2.792686
2000 2.168160
2001 2.542518
2002 2.375215
2004 2.630202
2005 3.243469
2006 1.930584
2007 2.542558
2008 2.498186
2009 3.197280
2010 2.771228
2011 2.011123
2012 1.723698
2013 1.947043
2014 2.148983
2015 2.426192
2016 2.517098
2017 2.064627
2018 2.085587
2019 2.503624
2020 2.565695
2021 2.289562
2022 2.412832
ggplot(summer_standard_dev_all_843, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 843 average summer temperatures for water years 2005-2021

summer MK & SS for 843 (non-corrected)

summer_sd_mk_843 <- mk.test(summer_standard_dev_all_843$sd_2)
print(summer_sd_mk_843)
## 
##  Mann-Kendall trend test
## 
## data:  summer_standard_dev_all_843$sd_2
## z = -2.9945, n = 34, p-value = 0.002749
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
## -203.0000000 4550.3333333   -0.3618538
summer_sd_sens_843 <- sens.slope(summer_standard_dev_all_843$sd_2)
print(summer_sd_sens_843)
## 
##  Sen's slope
## 
## data:  summer_standard_dev_all_843$sd_2
## z = -2.9945, n = 34, p-value = 0.002749
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.041071297 -0.008191406
## sample estimates:
## Sen's slope 
## -0.02257691

Winter

winter_standard_dev_all_843 <- standard_dev_843 %>%
  filter(waterDay >= 32 & waterDay <= 182) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())

winter_standard_dev_all_843 <- winter_standard_dev_all_843 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

winter_standard_dev_all_843 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 4.427912
1988 5.012550
1989 5.858939
1990 5.137108
1991 5.098992
1992 3.783912
1993 4.392078
1995 4.409859
1996 5.219231
1997 5.177420
1998 4.241123
1999 4.162789
2000 5.025710
2001 4.113427
2002 4.994185
2004 3.396785
2005 4.269988
2006 4.436911
2007 5.083808
2008 5.399585
2009 4.738283
2010 4.413961
2011 5.042425
2012 4.274299
2013 5.604427
2014 4.189625
2015 4.527692
2016 4.757855
2017 5.048261
2018 4.236822
2019 3.981781
2020 4.218900
2021 4.453029
2022 4.873308
ggplot(winter_standard_dev_all_843, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 843 average winter temperatures for water years 2005-2021

winter MK & SS for 843 (non-corrected)

winter_sd_mk_843 <- mk.test(winter_standard_dev_all_843$sd_2)
print(winter_sd_mk_843)
## 
##  Mann-Kendall trend test
## 
## data:  winter_standard_dev_all_843$sd_2
## z = -0.91912, n = 34, p-value = 0.358
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
##  -63.0000000 4550.3333333   -0.1122995
winter_sd_sens_843 <- sens.slope(winter_standard_dev_all_843$sd_2)
print(winter_sd_sens_843)
## 
##  Sen's slope
## 
## data:  winter_standard_dev_all_843$sd_2
## z = -0.91912, n = 34, p-value = 0.358
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.030910729  0.009292454
## sample estimates:
##  Sen's slope 
## -0.007865349

Summer and Winter SD CORRECTED

Summer

summer_standard_dev_all_843_ad <- standard_dev_843_ad %>% 
  filter(waterDay >= 244 & waterDay <= 335) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())
summer_standard_dev_all_843_ad <- summer_standard_dev_all_843_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
summer_standard_dev_all_843_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 2.105328
1988 2.357017
1989 2.872795
1990 2.756770
1991 2.511435
1992 2.759558
1993 3.123166
1995 3.304335
1996 2.386285
1997 2.428904
1998 3.100500
1999 2.519729
2000 1.940468
2001 2.284102
2002 2.126206
2004 2.355973
2005 2.945878
2006 1.930028
2007 2.542672
2008 2.499493
2009 3.195425
2010 2.767912
2011 2.010535
2012 1.723562
2013 1.947511
2014 2.148154
2015 2.427029
2016 2.515887
2017 2.063971
2018 2.086096
2019 2.502777
2020 2.565655
2021 2.289051
2022 2.411675
ggplot(summer_standard_dev_all_843_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 843 average summer temperatures for water years 1986-2021

summer MK & SS 843 (corrected)

summer_sd_mk_843_ad <- mk.test(summer_standard_dev_all_843_ad$sd_2)
print(summer_sd_mk_843_ad)
## 
##  Mann-Kendall trend test
## 
## data:  summer_standard_dev_all_843_ad$sd_2
## z = -1.3935, n = 34, p-value = 0.1635
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
##  -95.0000000 4550.3333333   -0.1693405
summer_sd_sens_843_ad <- sens.slope(summer_standard_dev_all_843_ad$sd_2)
print(summer_sd_sens_843_ad)
## 
##  Sen's slope
## 
## data:  summer_standard_dev_all_843_ad$sd_2
## z = -1.3935, n = 34, p-value = 0.1635
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.027423400  0.002497495
## sample estimates:
## Sen's slope 
## -0.01107592

Winter

winter_standard_dev_all_843_ad <- standard_dev_843_ad %>% 
  filter(waterDay >= 32 & waterDay <= 182) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())
winter_standard_dev_all_843_ad <- winter_standard_dev_all_843_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
winter_standard_dev_all_843_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 4.224648
1988 4.806735
1989 5.639223
1990 4.895536
1991 4.901330
1992 3.611549
1993 4.226666
1995 4.200474
1996 4.980076
1997 4.922782
1998 4.043928
1999 3.958859
2000 4.747752
2001 3.935747
2002 4.753771
2004 3.128328
2005 4.061855
2006 4.436952
2007 5.082876
2008 5.398178
2009 4.737126
2010 4.413584
2011 5.040957
2012 4.274462
2013 5.603638
2014 4.188733
2015 4.527289
2016 4.756252
2017 5.047793
2018 4.236955
2019 3.981614
2020 4.218761
2021 4.452850
2022 4.872764
ggplot(winter_standard_dev_all_843_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 843 average winter temperatures for water years 1986-2021

winter MK & SS 843 (corrected)

winter_sd_mk_843_ad <- mk.test(winter_standard_dev_all_843_ad$sd_2)
print(winter_sd_mk_843_ad)
## 
##  Mann-Kendall trend test
## 
## data:  winter_standard_dev_all_843_ad$sd_2
## z = 0.059298, n = 34, p-value = 0.9527
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
## 5.000000e+00 4.550333e+03 8.912656e-03
winter_sd_sens_843_ad <- sens.slope(winter_standard_dev_all_843_ad$sd_2)
print(winter_sd_sens_843_ad)
## 
##  Sen's slope
## 
## data:  winter_standard_dev_all_843_ad$sd_2
## z = 0.059298, n = 34, p-value = 0.9527
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.02034928  0.01882551
## sample estimates:
##  Sen's slope 
## 0.0004243853

Wolf Creek Summit 874

Morrisey 7/12/2004

snotel_874 <- SNOTEL_san_juan_Area %>% 
  filter(site_id == "874")
#str(snotel_874) # check the date, usually a character.  

snotel_874$Date <- as.Date(snotel_874$date) #change date from character to date format, capitalize to work with Water year functon from NWIS.

#THIS WILL CHANGE FOR EACH STATION
snotel_874_clean <- snotel_874 %>% # filter for the timeframe
  filter(Date >= "1979-10-01" & Date <= "2022-09-30") %>%
  #filter(temperature_mean >= -30 & temperature_mean <= 20) %>% # removing outliers   
  addWaterYear() %>% 
  mutate(daymonth = format(as.Date(Date), "%d-%m")) %>% 
  na.omit()

#adding water day using difftime (SUPER COOL. example from [this](https://stackoverflow.com/questions/48123049/create-day-index-based-on-water-year))

snotel_874_clean <- snotel_874_clean %>% 
  group_by(waterYear)%>% 
  mutate(waterDay = (as.integer(difftime(Date, ymd(paste0(waterYear - 1 ,'-09-30')), units = "days"))))
# Check for outliers

ggplot(snotel_874_clean, aes(x = Date, y = temperature_mean)) +
  geom_point() + #lwd = 2) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature (°C)') + 
  xlab('Date')

snotel_874_clean <- snotel_874_clean %>% 
  mutate(temp_diff = abs(temperature_min - temperature_max)) %>% 
  filter(temperature_mean > -40) %>% 
  filter(temperature_mean < 25)
ggplot(snotel_874_clean, aes(x = Date, y = temp_diff)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  #geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature varience (°C)') + 
  xlab('Date')

Per Steven’s advice: :If there are more than 15 missing days, then…remove that year”

# filtering for temperature anomalies
#snotel_874_cull_count <- snotel_874_clean %>% 
#  filter(temperature_min > -40) %>% 
#  count(waterYear)

#snotel_874_cull_count

# filtering for too few observations in a year
snotel_874_cull_count_days <- snotel_874_clean %>% 
  group_by(waterYear) %>% 
  count(waterYear) %>% 
  filter(n < 350)

snotel_874_cull_count_days
## # A tibble: 11 x 2
## # Groups:   waterYear [11]
##    waterYear     n
##        <dbl> <int>
##  1      1990   295
##  2      1994   313
##  3      2003   311
##  4      2006   340
##  5      2007   340
##  6      2008   315
##  7      2009   320
##  8      2010   303
##  9      2013   304
## 10      2016   338
## 11      2017   314

1990, 1994, 2003, 2006, 2007, 2008, 2009, 2010, 2013, 2016, 2017 need to be culled.

snotel_874_clean_culled <- snotel_874_clean %>% 
  filter(waterYear != "1990" & waterYear != "1994" & waterYear != "2003" & waterYear != "2006" & waterYear != "2007" & waterYear != "2008" & waterYear != "2009" & waterYear != "2010" & waterYear != "2013" & waterYear != "2016" & waterYear != "2017") #%>% 
  #filter(temperature_mean > -49)

ggplot(snotel_874_clean_culled, aes(x = Date, y = temp_diff)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  #geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature varience (°C)') + 
  xlab('Date')

ggplot(snotel_874_clean_culled, aes(x = Date, y = temperature_mean)) + 
  geom_point() + #lwd = 2) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('Daily temperature (°C)') + 
  xlab('Date')

temp_874_xts <- xts(snotel_874_clean_culled$temperature_mean, order.by = snotel_874_clean_culled$Date)

dygraph(temp_874_xts) %>%
  dyAxis("y", label = "Daily mean temperature (°C)") 
#snotel_874_clean_culled <- snotel_874_clean_culled %>% 
#  filter(temperature_mean < 19.3)

temp_874_xts <- xts(snotel_874_clean_culled$temperature_mean, order.by = snotel_874_clean_culled$Date)

dygraph(temp_874_xts) %>%
  dyAxis("y", label = "Daily mean temperature (°C)") 

Wolf Creek Summit 874

Morrisey 7/12/2004

snotel_874_adjusted <- snotel_874_clean_culled %>%
  mutate(temp_ad = if_else(Date < "2004-07-12", ((5.3*10^(-7))*(temperature_mean^(4))+(3.72*10^(-5))*(temperature_mean^(3))-(2.16*10^(-3))*(temperature_mean^(2))-(7.32*10^(-2))*(temperature_mean)+1.37)+temperature_mean, temperature_mean))

Non-corrected

874 Detrended

#using the clean culled df:

#average water year temperature

yearly_wy_aver_874 <- snotel_874_adjusted %>% 
  group_by(waterYear) %>% 
  mutate(aver_ann_temp = mean(temperature_mean))

#Average temperature by day for all water years:

daily_wy_aver_874 <- yearly_wy_aver_874 %>% 
  group_by(daymonth) %>% 
  mutate(aver_day_temp = mean(aver_ann_temp))

#average mean temperature by day for the period of record:

daily_wy_aver_874 <- daily_wy_aver_874 %>% 
  group_by(daymonth) %>% 
  mutate(all_ave_temp = mean(daily_wy_aver_874$aver_day_temp))

# try to show all years as means. 
daily_wy_aver2_874 <-daily_wy_aver_874 %>% 
  group_by(waterDay) %>%
  mutate(date_temp = mean(temperature_mean))
  

daily_wy_aver2_874$date_temp <- signif(daily_wy_aver2_874$date_temp,3) #reduce the sig figs


ggplot(daily_wy_aver2_874, aes(x = waterDay, y = date_temp))+
  geom_line(size= 0.7) +
  theme_few() +
  ylab('Average Daily temperature (°C)') + 
  xlab('Day of water year')

874 SD

standard_dev_874 <- daily_wy_aver_874 %>% 
  group_by(waterYear) %>% 
  mutate(residual = (all_ave_temp-aver_ann_temp)+temperature_mean-aver_day_temp) %>% 
  mutate(deviation = abs(residual-lag(residual)))

standard_dev_all_874 <- standard_dev_874 %>% 
  group_by(waterYear) %>% 
  mutate(nmbr = n())

standard_dev_all_874 <- standard_dev_all_874 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

standard_dev_all_874 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 7.854854
1988 8.380441
1989 8.376871
1991 7.952272
1992 7.274045
1993 7.924599
1995 7.536845
1996 8.020204
1997 8.005351
1998 8.406529
1999 7.057906
2000 8.042917
2001 8.360075
2002 8.824249
2004 8.065839
2005 7.482529
2011 8.051681
2012 7.789860
2014 7.447867
2015 6.928269
2018 7.223986
2019 7.800112
2020 7.863897
2021 8.007803
2022 7.236522
ggplot(standard_dev_all_874, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 874 average temperatures for water years 2005-2021

MK & SS for 874 (non-corrected)

sd_mk_874 <- mk.test(standard_dev_all_874$sd_2)
print(sd_mk_874)
## 
##  Mann-Kendall trend test
## 
## data:  standard_dev_all_874$sd_2
## z = -1.4247, n = 25, p-value = 0.1543
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
##  -62.0000000 1833.3333333   -0.2066667
sd_sens_874 <- sens.slope(standard_dev_all_874$sd_2)
print(sd_sens_874)
## 
##  Sen's slope
## 
## data:  standard_dev_all_874$sd_2
## z = -1.4247, n = 25, p-value = 0.1543
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.046813576  0.005791174
## sample estimates:
## Sen's slope 
## -0.01681136

Corrected

874 Detrended (corrected)

#using the clean culled df:
#average water year temperature
yearly_wy_aver_874_ad <- snotel_874_adjusted %>% 
  group_by(waterYear) %>% 
  mutate(aver_ann_temp_ad = mean(temp_ad))
#Average temperature by day for all water years:
daily_wy_aver_874_ad <- yearly_wy_aver_874_ad %>% 
  group_by(daymonth) %>% 
  mutate(aver_day_temp_ad = mean(aver_ann_temp_ad))
#average mean temperature by day for the period of record:
daily_wy_aver_874_ad <- daily_wy_aver_874_ad %>% 
  group_by(daymonth) %>% 
  mutate(all_ave_temp_ad = mean(daily_wy_aver_874_ad$aver_day_temp_ad))
# try to show all years as means. 
daily_wy_aver2_874_ad <-daily_wy_aver_874_ad %>% 
  group_by(waterDay) %>%
  mutate(date_temp_ad = mean(temp_ad))
  
daily_wy_aver2_874_ad$date_temp_ad <- signif(daily_wy_aver2_874_ad$date_temp_ad,3) #reduce the sig figs
ggplot(daily_wy_aver2_874_ad, aes(x = waterDay, y = date_temp_ad))+
  geom_line(size= 0.7) +
  theme_few() +
  ylab('Average Daily temperature (°C)') + 
  xlab('Day of water year')

874 SS (corrected)

standard_dev_874_ad <- daily_wy_aver_874_ad %>% 
  group_by(waterYear) %>% 
  #filter(waterYear >= 1987 & waterYear <= 2021) %>% 
  mutate(residual = (all_ave_temp_ad-aver_ann_temp_ad)+temp_ad-aver_day_temp_ad) %>% 
  mutate(deviation = abs(residual-lag(residual)))
standard_dev_all_874_ad <- standard_dev_874_ad %>% 
  group_by(waterYear) %>% 
  mutate(nmbr = n())
standard_dev_all_874_ad <- standard_dev_all_874_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
standard_dev_all_874_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 7.327962
1988 7.851838
1989 7.848099
1991 7.458480
1992 6.775174
1993 7.395057
1995 7.010946
1996 7.470539
1997 7.481361
1998 7.826754
1999 6.568878
2000 7.465159
2001 7.779500
2002 8.206390
2004 7.444487
2005 7.482090
2011 8.052315
2012 7.790524
2014 7.447936
2015 6.928554
2018 7.224892
2019 7.800202
2020 7.864350
2021 8.008044
2022 7.236992
ggplot(standard_dev_all_874_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 874 average temperatures for water years 1986-2021

MK & SS 874 (corrected)

sd_mk_874_ad <- mk.test(standard_dev_all_874_ad$sd_2)
print(sd_mk_874_ad)
## 
##  Mann-Kendall trend test
## 
## data:  standard_dev_all_874_ad$sd_2
## z = 0.81742, n = 25, p-value = 0.4137
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##        S     varS      tau 
##   36.000 1833.333    0.120
sd_sens_874_ad <- sens.slope(standard_dev_all_874_ad$sd_2)
print(sd_sens_874_ad)
## 
##  Sen's slope
## 
## data:  standard_dev_all_874_ad$sd_2
## z = 0.81742, n = 25, p-value = 0.4137
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.006890724  0.033504267
## sample estimates:
## Sen's slope 
## 0.006882736

Figure captions are not correct.

Summer and Winter SD NOT-CORRECTED

Summer

summer_standard_dev_all_874 <- standard_dev_874 %>%
  filter(waterDay >= 244 & waterDay <= 335) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())

summer_standard_dev_all_874 <- summer_standard_dev_all_874 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

summer_standard_dev_all_874 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 2.301386
1988 2.585726
1989 3.004368
1991 2.670905
1992 2.884099
1993 3.118708
1995 3.418628
1996 2.231442
1997 2.426972
1998 3.172632
1999 2.579648
2000 1.937835
2001 2.390252
2002 2.287826
2004 2.301057
2005 3.018852
2011 1.966234
2012 1.692489
2014 1.964946
2015 2.358728
2018 2.048511
2019 2.420984
2020 2.369083
2021 2.450898
2022 2.292576
ggplot(summer_standard_dev_all_874, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 874 average summer temperatures for water years 2005-2021

summer MK & SS for 874 (non-corrected)

summer_sd_mk_874 <- mk.test(summer_standard_dev_all_874$sd_2)
print(summer_sd_mk_874)
## 
##  Mann-Kendall trend test
## 
## data:  summer_standard_dev_all_874$sd_2
## z = -1.8918, n = 25, p-value = 0.05852
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
##  -82.0000000 1833.3333333   -0.2733333
summer_sd_sens_874 <- sens.slope(summer_standard_dev_all_874$sd_2)
print(summer_sd_sens_874)
## 
##  Sen's slope
## 
## data:  summer_standard_dev_all_874$sd_2
## z = -1.8918, n = 25, p-value = 0.05852
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.051900496  0.001114139
## sample estimates:
## Sen's slope 
##  -0.0235135

Winter

winter_standard_dev_all_874 <- standard_dev_874 %>%
  filter(waterDay >= 32 & waterDay <= 182) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())

winter_standard_dev_all_874 <- winter_standard_dev_all_874 %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)

winter_standard_dev_all_874 %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 4.061936
1988 4.832543
1989 5.451625
1991 4.691108
1992 3.473366
1993 3.766941
1995 3.954827
1996 4.732658
1997 4.845022
1998 4.074815
1999 4.075788
2000 4.927227
2001 3.797701
2002 4.992024
2004 5.237861
2005 3.970113
2011 5.050437
2012 4.286693
2014 4.030299
2015 4.576167
2018 4.016512
2019 3.833885
2020 4.088284
2021 4.399188
2022 4.638982
ggplot(winter_standard_dev_all_874, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Non-corrected standard deviation of SNOTEL 874 average winter temperatures for water years 2005-2021

winter MK & SS for 874 (non-corrected)

winter_sd_mk_874 <- mk.test(winter_standard_dev_all_874$sd_2)
print(winter_sd_mk_874)
## 
##  Mann-Kendall trend test
## 
## data:  winter_standard_dev_all_874$sd_2
## z = 0.11677, n = 25, p-value = 0.907
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##        S     varS      tau 
##    6.000 1833.333    0.020
winter_sd_sens_874 <- sens.slope(winter_standard_dev_all_874$sd_2)
print(winter_sd_sens_874)
## 
##  Sen's slope
## 
## data:  winter_standard_dev_all_874$sd_2
## z = 0.11677, n = 25, p-value = 0.907
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.03693833  0.02951488
## sample estimates:
## Sen's slope 
## 0.001119478

Summer and Winter SD CORRECTED

Summer

summer_standard_dev_all_874_ad <- standard_dev_874_ad %>% 
  filter(waterDay >= 244 & waterDay <= 335) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())
summer_standard_dev_all_874_ad <- summer_standard_dev_all_874_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
summer_standard_dev_all_874_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 2.067990
1988 2.333310
1989 2.697472
1991 2.412870
1992 2.603861
1993 2.815957
1995 3.077053
1996 2.001191
1997 2.188199
1998 2.857796
1999 2.330436
2000 1.735144
2001 2.153284
2002 2.049339
2004 2.145615
2005 3.017410
2011 1.967414
2012 1.690995
2014 1.962954
2015 2.357646
2018 2.047435
2019 2.420713
2020 2.368572
2021 2.450683
2022 2.292765
ggplot(summer_standard_dev_all_874_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 874 average summer temperatures for water years 1986-2021

summer MK & SS 874 (corrected)

summer_sd_mk_874_ad <- mk.test(summer_standard_dev_all_874_ad$sd_2)
print(summer_sd_mk_874_ad)
## 
##  Mann-Kendall trend test
## 
## data:  summer_standard_dev_all_874_ad$sd_2
## z = -0.81742, n = 25, p-value = 0.4137
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##        S     varS      tau 
##  -36.000 1833.333   -0.120
summer_sd_sens_874_ad <- sens.slope(summer_standard_dev_all_874_ad$sd_2)
print(summer_sd_sens_874_ad)
## 
##  Sen's slope
## 
## data:  summer_standard_dev_all_874_ad$sd_2
## z = -0.81742, n = 25, p-value = 0.4137
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.03172149  0.01524502
## sample estimates:
##  Sen's slope 
## -0.007710063

Winter

winter_standard_dev_all_874_ad <- standard_dev_874_ad %>% 
  filter(waterDay >= 32 & waterDay <= 182) %>%
  group_by(waterYear) %>% 
  mutate(nmbr = n())
winter_standard_dev_all_874_ad <- winter_standard_dev_all_874_ad %>% 
  group_by(waterYear) %>% 
  mutate(resid_mean = mean(residual)) %>%
  mutate(sd_1 = residual-resid_mean) %>% 
  mutate(sd_2 = (((sum((sd_1)^2))/((nmbr-1))))^(0.5)) %>%
  distinct(sd_2, .keep_all = TRUE) %>% 
   select(waterYear, sd_2)
winter_standard_dev_all_874_ad %>% 
  kable(.,'html') %>%
  kable_styling() %>%
  scroll_box(width='250px',height='500px')
waterYear sd_2
1987 3.930208
1988 4.700087
1989 5.293992
1991 4.570232
1992 3.352405
1993 3.649435
1995 3.820087
1996 4.575167
1997 4.673789
1998 3.930004
1999 3.919844
2000 4.703588
2001 3.676065
2002 4.802910
2004 5.055491
2005 3.969583
2011 5.051431
2012 4.288012
2014 4.029693
2015 4.575844
2018 4.016674
2019 3.833270
2020 4.087497
2021 4.399418
2022 4.638674
ggplot(winter_standard_dev_all_874_ad, aes(x = waterYear, y = sd_2))+
  geom_line(size= 0.7) +
  theme_few() +
  geom_smooth(method = "lm", se=FALSE) +
  ylab('SD') + 
  xlab('Water year')

Morrisey-corrected standard deviation of SNOTEL 874 average winter temperatures for water years 1986-2021

winter MK & SS 874 (corrected)

winter_sd_mk_874_ad <- mk.test(winter_standard_dev_all_874_ad$sd_2)
print(winter_sd_mk_874_ad)
## 
##  Mann-Kendall trend test
## 
## data:  winter_standard_dev_all_874_ad$sd_2
## z = 0.58387, n = 25, p-value = 0.5593
## alternative hypothesis: true S is not equal to 0
## sample estimates:
##            S         varS          tau 
## 2.600000e+01 1.833333e+03 8.666667e-02
winter_sd_sens_874_ad <- sens.slope(winter_standard_dev_all_874_ad$sd_2)
print(winter_sd_sens_874_ad)
## 
##  Sen's slope
## 
## data:  winter_standard_dev_all_874_ad$sd_2
## z = 0.58387, n = 25, p-value = 0.5593
## alternative hypothesis: true z is not equal to 0
## 95 percent confidence interval:
##  -0.02540713  0.03689025
## sample estimates:
## Sen's slope 
## 0.007514331